systemd 中的守护程序服务



我已经设法在/etc/systemd/system中安装了守护程序服务,但是我不确定两件事:

  1. 守护程序服务是否应驻留在那里

  2. 如何优雅地检查守护程序服务是否已在 systemd 中安装?

1.守护程序服务是否应驻留在那里

是的,它是 .service 位置。您应该放在这里的文件是:

我的迪蒙服务

[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=**YourUser**
ExecStart=**pathToYourScript**
[Install]
WantedBy=multi-user.target

您需要:

  • 在用户= 之后设置您的实际用户名
  • 在 ExecStart= 中设置脚本的正确路径(通常为/usr/bin/你可以把你的脚本放在这里(

创建 linux-service-with-systemd

2.如何优雅地检查 systemd 中是否安装了守护程序服务?

systemctl 为此有一个 is-active 子命令:

systemctl is-active --quiet service

如果服务处于活动状态,则以状态零退出,否则为非零,使其成为脚本的理想选择:

systemctl is-active --quiet service && echo Service is running

测试服务正在运行

最新更新