Custom systemctl management service

Keywords: Linux crontab DBus Docker

Brief introduction
systemd is to control power on service, power on level and other functions by means of management unit.
In / usr/lib/systemd/system directory, there are various unit files, including service unit with service suffix, power on level unit with target suffix, etc. here are the files about service suffix. Because system D needs to perform self startup when it is started, it is controlled by the unit of these *. Services. Services are divided into system services and user services.

  • System service: a program that can be run without logging on (usually used for booting).
  • User service: a program that needs to be logged in to run.

Profile description:

All *. service files are stored in / lib/systemd/system directory,
We can check the crontab.service file to see what's written in it

[root@zhangsf system]# cd ~
[root@zhangsf ~]# cat /usr/lib/systemd/system/crond.service
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target

It can be seen that it is roughly divided into the following parts
*[Unit] block: start order and dependency

Description: a brief introduction to the current service
 Documentation: where to use documents 
After: if the audited.service or systemd-user-sessions.service service service needs to be started, it should be started before this service
 Before: crontd services should be started before those services

Note: After and Before only involve startup sequence, not dependency

[Service] start behavior

Start command
 >Execstart field: defines the command to execute when starting the process
 >Execreload field: command to be executed when the service is restarted
 >Execstop field: command executed when the service is stopped
 >Execstartpre field: command executed before starting the service
 >Execstartpost field: command executed after starting the service
 >Execstoppost field: command executed after stopping the service

Note: before all startup settings, a hyphen (-) can be added to indicate "suppression error", that is, when an error occurs, the execution of other commands will not be affected.
For example, EnvironmentFile=-/etc/sysconfig/sshd (note the hyphen after the equal sign), which means that even if
 /The etc/sysconfig/sshd file does not exist and will not throw errors.
Note: the start, restart, and stop commands in [Service] all require absolute paths!

startup type
 The Type field defines the startup Type. The values it can set are as follows:
>Simple (default): the process started by the ExecStart field is the main process
 >Forking: the ExecStart field will be started in the form of fork(). At this time, the parent process will exit and the child process will become the main process (running in the background)
>Oneshot: similar to simple, but only once. Systemd will wait for it to finish executing before starting other services
 >DBUS: similar to simple, but will wait for D-Bus signal to start
 >Notify: similar to simple, a notification signal will be sent after startup, and then Systemd will 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 mix with the output of other services

Restart behavior
 The Service block has some fields that define the restart behavior:
>Killmode field: defines how Systemd stops the sshd service:
>Control group (default): all subprocesses 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.
>Restart field: defines the restart mode of system d after sshd exits
 In the above example, Restart is set to on failure, which means that any unexpected failure will Restart sshd. If sshd stops normally (such as executing the systemctl stop command), it will not Restart.
>>The values that can be set for the restart field are as follows.
>>No (default): no reboot after exit
 >>On success: it will restart only when it exits normally (exit status code is 0)
>>On failure: when abnormal exit (exit status code is not 0), including signal termination and timeout, it will restart
 >>On abnormal: restart only after signal termination and timeout
 >>On abort: it will restart only when the signal not captured is terminated
 >>On watchdog: exit after timeout, and then restart
 >>Always: no matter what the reason is, always restart
 Note: for daemons, on failure is recommended. For services that allow error exits, you can set on-abnormal.
RestartSec field: indicates the number of seconds to wait before system D restarts the service.
The above example is set to wait 42 seconds.

[install] how to install this profile
WantedBy field: indicates the Target of the service.
Target means a service group, which means a group of services.
WantedBy=multi-user.target means that the Target of sshd is multi-user.target.
This setting is very important because when executing the systemctl enable sshd.service command, a symbolic link of sshd.service will be placed in the multi-user.target.wants subdirectory under the / etc/systemd/system directory.
Systemd has a default start Target.

systemctl get-default
#Output multi-user.target

The above result indicates that the default startup Target is multi-user.target. All services in this group will be started. This is why the systemctl enable command can be used to set the startup.
When using Target, the systemctl list dependencies command and systemctl isolate command are also useful.

#View all services included in multi-user.target
systemctl list-dependencies multi-user.target

#Switch to another target
#shutdown.target is the shutdown status
systemctl isolate shutdown.target

Generally speaking, there are two common targets:
multi-user.target: indicates the status of multi-user command line;
Graphic.target: indicates the status of the graphic user, which depends on multi-user.target.

An example of an example

[root@zhangsf system]# cat /usr/lib/systemd/system/node-exporter.service
[Unit]
Description=This is prometheus node exporter
After=docker.service

[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target

Executive order

systemctl daemon-reload
systemctl enable node-exporter.service
systemctl start node-exporter.service

view log

[root@zhangsf system]# tail -f /var/log/messages
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - sockstat" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - stat" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - textfile" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - time" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - timex" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - uname" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - vmstat" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - xfs" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg=" - zfs" source="node_exporter.go:104"
Feb  6 11:53:40 zhangsf node_exporter: time="2020-02-06T11:53:40+08:00" level=info msg="Listening on :9100" source="node_exporter.go:170"

Reference resources:

https://www.jianshu.com/p/79059b06a121

Posted by baennaeck on Thu, 06 Feb 2020 01:46:46 -0800