1、添加服务:
/usr/lib/systemd/system/下添加xxx.service,内容如下:
[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、设置开机自启动(任意目录下执行)。如果执行启动命令报错,则执行:systemctl daemon-reload
设置开机自启动
[root@localhost ~]# systemctl enable xxx
停止开机自启动
[root@localhost ~]# systemctl disable xxx
验证一下是否为开机启动
[root@localhost ~]# systemctl is-enabled xxx
3、其他命令
启动服务:
systemctl start xxx
停止服务:
systemctl stop xxx
重启服务:
systemctl restart xxx
查看nginx服务当前状态
[root@localhost ~]# systemctl status xxx
查看所有已启动的服务
[root@localhost ~]# systemctl list-units --type=service
查看具体服务的配置:
systemctl show xxx
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参考链接: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
相关参考文章:
看门狗相关:
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
源代码:
https://github.com/systemd/systemd
比较全面的参考文章:
https://cloud.tencent.com/developer/article/1516125
systemd调试:
https://blog.csdn.net/liumiaocn/article/details/89086548