Use of linux service service (including watchdog)

1. Add service:

Add xxx.service under /usr/lib/systemd/system/, the content is as follows:

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
 
[Service]服务运行参数的设置
Type=forking      是后台运行的形式
ExecStart        为服务的具体运行命令
ExecReload       为服务的重启命令
ExecStop        为服务的停止命令
Restart         为服务的重启命令
PrivateTmp=True     表示给服务分配独立的临时空间
PIDFile          为服务监测的pid文件
WatchdogSec=60s  启动服务的看门狗(超时时间为60s,需daemon进程使用sd_notify定时喂狗,否则看门狗超时会杀掉daemon进程)
NotifyAccess=main  设置只有daemon进程的喂狗信号才可以被处理
...
注意:启动、重启、停止命令全部要求使用绝对路径
 
[Install]        服务安装的相关设置,可设置为多用户
WantedBy=multi-user.target 

2. Set up auto-start (execute in any directory). If an error is reported when executing the startup command, execute: systemctl daemon-reload

Set boot up automatically
[root@localhost ~]# systemctl enable xxx

Stop self-starting after booting
[root@localhost ~]# systemctl disable xxx

Verify whether it is booting
[root@localhost ~]# systemctl is-enabled xxx

3. Other commands

Start the service:
systemctl start xxx

Stop the service:
systemctl stop xxx

Restart the service:
systemctl restart xxx

View the current status of nginx service
[root@localhost ~]# systemctl status xxx

View all started services
[root@localhost ~]# systemctl list-units --type=service

View the configuration of specific services:
systemctl show xxx

The difference between type=forking, notify, oneshort, simple, dbus:

The type of the service
Systemd defines and distinguish between some different type of services depending on their expected behavior. The type of a service can be defined by using the Type option, providing one of these values:

simple
forking
oneshot
dbus
notify
The default type of a service, if the Type and Busname options are not defined, but a command is provided via the ExecStart option, is simple. When this type of service is set, the command declared in ExecStart is considered to be the main process/service.

The forking type works differently: the command provided with ExecStart is expected to fork and launch a child process, which will become the main process/service. The parent process it's expected to die once the startup process is over.

The oneshot type is used as the default if the Type and ExecStart options are not defined. It works pretty much like simple: the difference is that the process is expected to finish its job before other units are launched. The unit, however, it's still considered as "active" even after the command exits, if the RemainAfterExit option is set to "yes" (the default is "no").

The next type of service is dbus. If this type of service is used, the daemon is expected to get a name from Dbus, as specified in the BusName option, which in this case, becomes mandatory. For the rest it works like the simple type. Consequent units, however, are launched only after the DBus name is acquired.

Another process works similarly to simple, and it is notify: the difference is that the daemon is expected to send a notification via the sd_notify function. Only once this notification is sent, consequent units are launched.

type reference link: https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux#:~:text=How%20to%20create%20systemd%20service%20unit%20in%20Linux,Creating %20and%20installing%20a%20service%20unit%209%20Conclusions

Related reference articles:
Watchdog related:
https://gohalo.me/post/linux-systemd-notify-watchdog-introduce.html
https://www.man7.org/linux/man-pages/man3/sd_notify. 3.html
https://www.man7.org/linux/man-pages/man3/sd_watchdog_enabled.3.html
https://www.man7.org/linux/man-pages/man5/systemd.service. 5. html
https://manpages.debian.org/jessie/systemd/systemd.service.5.en.html
Source code:
https://github.com/systemd/systemd
More comprehensive reference article:
https://cloud. tencent.com/developer/article/1516125
systemd debugging:
https://blog.csdn.net/liumiaocn/article/details/89086548

Guess you like

Origin blog.csdn.net/sun172270102/article/details/109296808