【问题标题】:Daemon service in systemdsystemd 中的守护进程服务
【发布时间】:2018-08-30 10:35:14
【问题描述】:

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

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

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

【问题讨论】:

标签: linux systemd


【解决方案1】:

1.如果守护程序服务应该驻留在那里

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

mydeamon.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

您需要:

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

creating-a-linux-service-with-systemd

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

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

systemctl is-active --quiet service

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

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

test Service is running

【讨论】:

    最近更新 更多