[Linux] Use systemd to set the boot self-start command

1 Use systemd to realize the automatic running command after booting

systemd is a modern init system in Linux systems that can be used to automatically run commands when booting. In systemd, you can create a service file, put the command to be executed in it, and then add it to the self-starting item of systemd.

The specific operation steps are as follows:

1.1 Create a new .service file

First create a .service file with sudo privileges in the terminal to store the service configuration:

sudo vim /etc/systemd/system/myservice.service

1.2 Write .service file

Add the following content to the opened file (the self-starting command demonstrated here requires sudo authority ), and replace ExecStart with the specific command that needs to be started automatically, and add sudo after ExecStart.

[Unit]
Description=My Service
After=network.target

[Service]
Type=simple
ExecStart=sudo /path/to/my/command
User=root

[Install]
WantedBy=multi-user.target

insert image description here

1.2.1 [Unit]

[Unit] is a part of the systemd service file, which is used to define the basic information of the service.

  • The Description field contains a short description, which is used to describe the name of the service, so that users can quickly understand the function of the service when searching.

  • After=network.target indicates that this service will start after the network service starts.

    • network.target refers to the network target unit, which is a special unit predefined in Systemd, and is used to represent the startup status of network services. Almost all network services, such as DHCP, DNS resolution, etc., depend on the availability of network services at startup. Therefore, adding After=network.target to the service's unit file ensures that the service starts after the network service is ready, thus avoiding dependency errors at startup.

1.2.2 [Service]

[Service] is another section in the systemd service file, which is used to define the specific behavior of the service.

  • The Type field is the runtime type of the service, and simple indicates that it is a background process, usually used for services with only one process.
  • The ExecStart field contains the command to be run, and multiple commands can be separated by a newline (\n).
  • The User field specifies which user to run the service as, here select the root user.

1.2.3 [Install]

[Install] defines the installation information of the systemd service.

  • The WantedBy field specifies the "target" (an operating system run level) that the service will execute. The "multi-user.target" specified here is the default run level of the Linux system that includes multiple users, which is equivalent to the traditional "init 3" runlevel. This means that the service will automatically run when the system starts.

1.3 Start the service and set up self-start

Save and exit the above .service file, the following command can start the service and add it to the system self-startup item:

sudo systemctl start myservice
sudo systemctl enable myservice

After executing the command, you can use the systemctl command to view the status of the service:

sudo systemctl status myservice.service

Once the service is started, it will start automatically on system boot. You can also stop the service and remove it from autostart at any time with the following command:

sudo systemctl stop myservice
sudo systemctl disable myservice

2 The main points of writing Systemd service files

2.1 Location of Systemd service files

Systemd service files are usually stored in the /etc/systemd/system directory. This directory contains various types of service files, such as system services, user services, network and so on.

2.2 Format of Systemd service file

Systemd service files are usually text files ending in .service. The various settings in the service file are specified in the form of key-value pairs, and each line is composed of key=value.

2.3 Basic structure of Systemd service file

Systemd service files usually contain the following configuration items:

  • [Unit]: Global information and dependency declaration of the service, such as service name, description, etc.

  • [Service]: Specify the specific configuration of the service, such as the command executed by the service, working directory, etc.

  • [Install]: Specify the installation method of the service, such as the startup level of the service, etc.

2.3.1 Configuration of [Unit] field

  • Description: A short description of the service.

  • Before: Defines that the service starts before other services.

  • After: Defines that the service starts after other services.

  • Requires: Defines which other services need to be started for the service to start, otherwise it cannot be started.

  • PartOf: Defines that the service is a part of other services, and if the other services stop, the service will also stop.

  • Wants: Defines which other services can be started at the same time as the service starts.

  • Condition...: Define the conditions for starting the service, such as ConditionPathExistsindicating that the service is started only when a certain path exists.

2.3.2 Configuration of [Service] field

  • Type: Service type, which can be simple, forking, ondemand, notify, etc.

  • ExecStart: Service startup command, which can be a single command, a script file, or a script composed of multiple commands.

  • ExecStop: Command to stop the service.

  • User: Defines the user the service runs as.

  • Group: Defines the user group under which the service runs.

  • PrivateTmp: Mount the service's /tmp directory into a private namespace to enhance security.

  • Restart: Define how to restart the service when it exits abnormally.

  • WorkingDirectory: Defines the service working directory.

  • Environment: Define the environment variables of the service, etc.

  • ProtectSystem: Prevents services from modifying system files.

  • NoNewPrivileges: Prevent services from escalating privileges through setuid or setgid etc.

2.3.3 Configuration of [Install] field

  • WantedBy: Defines under which system runlevels this service is enabled.

  • RequiredBy: This service must be started when starting other system services.

2.4 Sample .service file

[Unit]
Description=MyService
After=network.target
  
[Service]
Type=simple
ExecStart=/usr/bin/myservice
WorkingDirectory=/var/myservice
User=myservice
Group=myservice
Restart=always
RestartSec=30
StartLimitInterval=400
StartLimitBurst=3
  
[Install]
WantedBy=multi-user.target

In this example, a service named "MyService" is defined. When the service starts, the /usr/bin/myservice command is executed, the execution working directory is /var/myservice, and the running user is myservice. If the service exits abnormally, it will try to restart every 30 seconds, up to 3 times. Finally, the service will be enabled under the multi-user runlevel.

Guess you like

Origin blog.csdn.net/qq_41084756/article/details/130200523