单位文件
[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文件?