Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of creating custom scripts and systemd service unit files in Linux

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Today, I will talk to you about the method of creating custom scripts and systemd service unit files in Linux, which may not be well understood by many people. In order to make you understand better, the editor summarizes the following contents for you. I hope you can get something according to this article.

Preface

Systemd is the system and service manager of the Linux operating system. When you install any application from the repository, it places the service unit files in the systemd directory, and you should not modify them directly.

The systemd unit file will be found in the following three directories:

/ usr/lib/systemd/system/: the systemd unit file that was deleted when the package was installed.

The systemd unit file created by / run/systemd/system/: at run time.

/ etc/systemd/system/: systemd unit files created by the "systemctl enable" command and unit files added to extend the service.

Sometimes you may need to create a service unit file for a custom application or daemon or script. There are many parameters to add, but we will only add a few values to make the unit file simpler for better understanding.

For example, to run a custom script when the systemd system starts, you need to create a custom service unit file.

Create a custom script

The following shell script writes a welcome message to the file, as follows:

$sudo vi / usr/sbin/welcome.sh

#! / bin/bash

Echo 'Welcome to Linux WorldWORLD.popular stories' > > / tmp/welcome.txt

Reference: two ways to run .sh files on Linux systems.

Create a systemd unit file

You need to create a custom service unit file in the "/ etc/systemd/system/" directory because this is reserved for custom scripts. Any unit file in'/ etc/systemd/system' will overwrite the corresponding file in'/ lib/systemd/system'.

Syntax: the systemd unit file consists of three parts:

Section-1 [Unit] Parameter 1..Parameter NSection-2 [Service] Parameter 1..Parameter NSection-3 [Install] Parameter 1

To demonstrate this, we will create a systemd service unit file named "custom.service":

$sudo vi / etc/systemd/system/custom.service

[Unit]

Description=example systemd custom service unit file

After=network.target

[Service]

Type=notify

ExecStart=/bin/bash / usr/sbin/welcome.sh

[Install]

WantedBy=multi-user.target

1 、 Section-1:

Unit: this section provides basic information about services.

Description: a short description of the service unit. When you execute the "systemctl status UNIT.service" command, the description appears next to the service unit name.

After: defines the order in which the unit starts. The "custom.service" unit starts only after the "network.target" unit starts.

2 、 Section-2:

Service: the "Service" section provides instructions on how to control the service.

Type: defines the type of systemd service. It is the same as "Type=simple", but at the same time the daemon wants to signal the systemd when it is ready.

ExecStart: this is used to start the service, including the full path to the actual service executable.

3 、 Section-3:

The Install: "Install" section provides instructions on how to install the systemd service.

WantedBy: the "WantedBy" setting indicates under which target the given service unit should be started. In this example, custom.service uses multi-user.target, so systemd starts custom.service when it loads multi-user.target at startup.

Set executable permissions to "custom.service":

$sudo chmod axix / etc/systemd/system/custom.service

To add a new service to systemd, run:

$sudo systemctl daemon-reload

To start custom.service, run:

$sudo systemctl start custom.service

To enable custom.service at startup, run:

$sudo systemctl enable custom.service

Finally, restart the system to check that custom.service runs the script as expected at startup by verifying the output file:

$sudo reboot

Yes, it works well:

$cat / tmp/welcome.txt

Welcome to Linux WORLD..!!!

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

After reading the above, do you have any further understanding of how to create custom scripts and systemd service unit files in Linux? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report