systemd service details

Module overview

[Unit] section

It mainly describes the description, content, document introduction and some dependent service definitions of the service

  • Description: description information
  • After: indicates the services that need to be relied on, and the function determines the start order
  • Before: indicates the dependent service
  • Requles: other units that are dependent on. Strong dependency means that the dependent unit fails to start. The unit does not start.
  • Wants: other units that depend on, weak dependence, that is, the dependent unit fails to start. The unit continues to start
  • Conflicts: define conflicting relationships

Handling dependencies

use systemd The dependency can be solved by writing the unit configuration file correctly. Typically, units A Requirement unit B stay A Run before startup. In this case, the unit A In the configuration file [Unit] Segment addition Requires=B and After=B Just. If this dependency is optional, it can be added Wants=B and After=B. 

Please note that Wants= and Requires= Doesn't mean After=,That is, if After= If the option is not specified, the two units will be started in parallel.

Dependencies are often used in services( service)Not the goal( target)Come on. For example, network.target Generally, it will be introduced by a service configured with a network interface. Therefore, it is OK to arrange the customized unit after the service because network.target Already started.

[Service] section

The main body definition of a service mainly defines some operation parameters and operation actions of the service

  • Type:

    • simple: the default value is to execute the command specified by ExecStart and start the main process
    • forking: create a child process from the parent process by fork ing. After creation, the parent process will exit immediately and the child process will become the main process
    • oneshot: a one-time process, similar to simple, but executed only once. System D will wait until the current service exits before continuing
    • dbus: the current service is started through D-Bus, similar to simple, but it will start after waiting for the D-Bus signal
    • Notify: after the current service is started, a notification signal will be sent to notify Systemd, and then Systemd will continue to start other services
    • idle: similar to simple, but the service will not be started until other tasks are completed. One use case is to make the output of this service not mixed with the output of other services
  • User: Specifies the user name to run the program automatically after startup

  • Group: Specifies the user group to run the program automatically after startup

  • LimitCORE=infinity: limits the size of kernel files

  • LimitNOFILE=65536: the maximum number of file descriptors allowed to be opened by the service

  • LimitNPROC=65536: maximum number of processes

  • PIDFile: Specifies the pid file for automatically running the program after startup (this item is generally configured in the program configuration file)

  • ExecStart: command to start the current service

  • ExecStartPre: command executed before starting the current service

  • ExecStartPost: command executed after starting the current service

  • ExecReload: command executed when restarting the current service

  • ExecStop: command executed when stopping the current service

  • ExecStopPost: command executed after stopping its service

  • KillMode: defines how to stop the service. The values that can be set for the KillMode field are as follows

    • Control group (default): all child processes in the current control group will be killed;
    • Process: kill only the main process;
    • mixed: the main process will receive SIGTERM signal, and the sub process will receive SIGKILL signal;
    • none: no process will be killed, just execute the stop command of the service. If the ssh service sets the KillMode to process and does not stop any sshd sub processes, that is, the ssh sessions opened by the sub processes still remain connected. This setting is not very common, but it is very important for sshd. Otherwise, when you stop the service, you will kill the ssh sessions opened by yourself.
  • KillSignal: sets the signal used in the first step of killing the process. The default value is SIGTERM signal.

  • RestartSec: the number of seconds to automatically restart the current service

  • Restart: defines the restart mode of system d after the current service exits. Possible values include

    • No (default): it will not restart after exiting;
    • Always: always restart no matter what the exit reason is;
    • On success: restart only after normal exit (exit status code is 0);
    • On failure: restart only after abnormal exit (exit status code is not 0), including signal termination and timeout;
    • On abnormal: restart only after being terminated and timed out by the signal;
    • On abort: restart only after receiving the termination signal that is not captured;
    • On watchdog: it will restart only after quitting after timeout. For example, if the ssh service is set to on failure, it means that sshd will be restarted in case of any unexpected failure. If sshd stops normally (such as executing the systemctl stop command), it will not restart.
  • RemainAfterExit: the value is yes or no, indicating that the service will still be executed after the process exits. In this way, once the service is stopped by using the systemctl stop command, the command specified by ExecStop will be executed

  • TimeoutSec: defines the number of seconds that Systemd waits before stopping the current service

  • Environment: Specifies the environment variable of the current service

  • EnvironmentFile: Specifies the environment parameter file of the current service. The key=value pair of the file can be obtained in the current configuration file in the form of $key

  • A hyphen (-) can be added to all startup settings to indicate "suppress error", that is, when an error occurs, it will not affect the execution of other commands. For example, EnvironmentFile=-/etc/sysconfig/sshd means that an error will not be thrown even if the / etc/sysconfig/sshd file does not exist.

  • If the value of PrivateTmp is set to true, a system like d-private-433ef27ba3d46d8aac286aeb1390e1b nginx will be generated in the / tmp directory when the service is started The folder of service redvyu is used to store temporary files of nginx.

[Install] section

The settings related to service installation can generally be set to multi-user settings

  • WantedBy: its value is one or more targets. When the current Unit is activated (enable), the symbolic link will be placed under the / etc/systemd/system directory with the Target name + In the subdirectory composed of the wants suffix
  • RequiredBy: its value is one or more targets. When the current Unit is activated, the symbolic link will be placed under the / etc/systemd/system directory with the name of Target + In the subdirectory composed of the required suffix
  • Alias: alias that the current Unit can use to launch
  • Also: other units that will be activated at the same time when the current Unit is activated (enabled)

Configuration file example

sshd service

[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

nginx service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Tomcat service

## System installation
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=simple
EnvironmentFile=/etc/tomcat/tomcat.conf
Environment="NAME="
EnvironmentFile=-/etc/sysconfig/tomcat
ExecStart=/usr/libexec/tomcat/server start
SuccessExitStatus=143
User=tomcat

[Install]
WantedBy=multi-user.target


## Binary installation
[Unit]
Description=tomcat
After=network.target

[Service]
Type=forking
Environment="export JAVA_HOME=/opt/jdk"
Environment="export JAVA_BIN=$JAVA_HOME/bin"
Environment="export JRE_HOME=$JAVA_HOME/jre"
Environment="export CLASSPATH=$JAVA_HOME/jre/lib:$JAVA_HOME/lib"
Environment="export PATH=$PATH:$JAVA_HOME/bin"
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

java services

[Unit]
Description=Manage Java service
After=network.target

[Service]
Type=simple
WorkingDirectory=/app
#User=root
#Group=root
ExecStart=/usr/java/jdk_1.8/jdk1.8.0_271/bin/java -Xms64m -Xmx612m -XX:PermSize=64m -XX:MaxPermSize=128m -jar online-server-1.0-SNAPSHOT.jar
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
StartLimitInterval=300
StartLimitBurst=3

[Install]
WantedBy=multi-user.target

StartLimitInterval=300
StartLimitBurst=3

It means that if the service is restarted more than 3 times within 5 minutes, it will not be restarted again

kill and semaphore

TERM

  • Sends a TERM signal to the specified process. If the process does not capture the signal, the process terminates
  • If no signal is specified, the TERM signal is sent. The TERM signal will kill processes which do not catch this signal.

Syntax format

kill -s TERM <pid>

systemd usage
ExecStop=/bin/kill -s TERM $MAINPID

Friendly tell the process to exit. The process saves the data first, and then exits normally.
Send a TERM signal to the parent process and try to kill it and its child process.
Request a complete termination of an execution It expects the receiving process to clear the self-sufficient state and exit

HUP

  • Let the Linux execution process shut down and then restart. This signal can be sent when the process needs to be restarted after modifying the configuration file.

Syntax format

kill -s HUP <pid>

systemd usage
ExecReload=/bin/kill -s HUP $MAINPID

QUIT

Equivalent to Ctrl + C

systemd usage
ExecStop=/bin/kill -s QUIT $MAINPID

Keywords: Linux CentOS systemd

Added by Shaba1 on Sun, 26 Dec 2021 04:42:21 +0200