单元文件
[root@myserver system]# cat /etc/systemd/system/thiru.service
[Unit]
Description=My service
[Service]
Type=forking
PIDFile=/var/run/thiru.pid
ExecStart=/tmp/thiru.sh start
ExecStop=/tmp/thiru.sh stop我的脚本
[root@myserver system]# cat /tmp/thiru.sh
#!/bin/sh
loop()
{
while true
do
sleep 5
done
}
if [ "$1" = "start" ]; then
loop &
echo "$!" > /var/run/thiru.pid
fi
if [ "$1" = "stop" ]; then
kill $(cat /var/run/thiru.pid)
rm -f /var/run/thiru.pid
fi现在,当我执行systemctl start thiru.service时,它工作得很好。但是,当我通过直接调用脚本/tmp/thiru.sh start启动服务时,systemctl不会检测到这一点。
[root@myserver system]# /tmp/thiru.sh start
[root@myserver system]# systemctl status thiru.service
thiru.service - My service
Loaded: loaded (/etc/systemd/system/thiru.service; static)
Active: inactive (dead)
Jul 27 04:14:08 myserver systemd[1]: Starting My service...
Jul 27 04:14:08 myserver systemd[1]: Started My service.
Jul 27 04:14:17 myserver systemd[1]: Stopping My service...
Jul 27 04:14:17 myserver systemd[1]: Stopped My service.是否有一种方法可以让systemd检测我的服务已经启动?可能用PID文件?
发布于 2015-08-08 15:33:58
systemd只监视它自己启动的进程。
您不再需要在脚本中编写特定的开始/停止函数。只需在ExecStart=中调用脚本的主要部分即可。
你不需要PID文件了。systemd可以使用cgroup跟踪脚本生成的所有进程。如果systemctl stop thiru没有杀死所有进程,那么systemctl kill --signal=term thiru就会执行它。
https://stackoverflow.com/questions/31648100
复制相似问题