我正在尝试编写一个脚本来检查服务是否正在运行(在本例中为 sickchill),如果没有,则使用 systemctl 启动它。
如果我从终端执行代码,它可以按预期工作,但是当我从 bash 脚本尝试执行代码时,如果服务已在运行,即使它没有运行,它也会起作用...
我不知道为什么会这样,因为代码仍然是一样的。
有什么想法可以解释为什么会这样吗?
我的shell命令和结果:
user@plex-webserver § ~ sudo /opt/scripts/check-sickchill-service
sickchill.service is running
user@plex-webserver § ~ sudo systemctl status sickchill.service
● sickchill.service - SickChill Daemon
Loaded: loaded (/etc/systemd/system/sickchill.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Sat 2020-09-19 08:11:31 UTC; 32s ago
Process: 636533 ExecStart=/usr/bin/python3 /opt/sickchill/SickChill.py -q --daemon --nolaunch --datadir=/opt/sickchill (code=exited, status=0/SUCCESS)
Sep 19 08:10:08 plex-webserver systemd[1]: Starting SickChill Daemon...
Sep 19 08:10:10 plex-webserver systemd[1]: Started SickChill Daemon.
Sep 19 08:10:10 plex-webserver python3[636549]: /opt/sickchill/sickchill/locale
Sep 19 08:10:10 plex-webserver python3[636549]: /opt/sickchill/sickchill/locale
Sep 19 08:10:10 plex-webserver python3[636549]: /opt/sickchill/sickchill/locale
Sep 19 08:11:20 plex-webserver systemd[1]: Stopping SickChill Daemon...
Sep 19 08:11:31 plex-webserver systemd[1]: sickchill.service: Succeeded.
Sep 19 08:11:31 plex-webserver systemd[1]: Stopped SickChill Daemon.
user@plex-webserver § ~ if (( $(ps -ef | grep -v grep | grep $SERVICE | wc -l) > 0 ))
then
echo "$SERVICE is running"
else
systemctl start $SERVICE
echo "restarted $SERVICE"
fi
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to start 'sickchill.service'.
Authenticating as: Username (user)
Password:
==== AUTHENTICATION COMPLETE ===
restarted sickchill
我的脚本(check-sickchill-service):
#!/bin/bash
SERVICE=sickchill.service
if (( $(ps -ef | grep -v grep | grep $SERVICE | wc -l) > 0 ))
then
echo "$SERVICE is running"
else
systemctl start $SERVICE
echo "restarted $SERVICE"
fi