1. Agregar servicio:
Agregue xxx.service en / usr / lib / systemd / system /, el contenido es el siguiente:
[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. Configure el inicio automático (ejecute en cualquier directorio). Si se informa de un error al ejecutar el comando de inicio, ejecute: systemctl daemon-reload
Configurar el arranque automáticamente
[root @ localhost ~] # systemctl enable xxx
Detener el inicio automático después de arrancar
[root @ localhost ~] # systemctl disable xxx
Verifique si está arrancando
[root @ localhost ~] # systemctl is-enabled xxx
3. Otros comandos
Inicie el servicio:
systemctl start xxx
Detenga el servicio:
systemctl stop xxx
Reinicie el servicio:
systemctl restart xxx
Ver el estado actual del servicio nginx
[root @ localhost ~] # systemctl status xxx
Ver todos los servicios iniciados
[root @ localhost ~] # systemctl list-units --type = service
Ver la configuración de servicios específicos:
systemctl show xxx
La diferencia entre type = bifurcación, notificar, onehort, 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.
Tipo de enlace de referencia: https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux#:~:text=How%20to%20create%20systemd%20service%20unit%20in%20Linux,Creating % 20y% 20instalación% 20a% 20servicio% 20unidad% 209% 20Conclusiones
Artículos de referencia
relacionados: Relacionados con el perro guardián:
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
Código fuente:
https://github.com/systemd/systemd
Artículo de referencia más completo:
https: // cloud. tencent.com / developer / article / 1516125
depuración de systemd:
https://blog.csdn.net/liumiaocn/article/details/89086548