重新启动 Raspberry Pi 如果服务不是 运行
Reboot Raspberry Pi if service not running
我正在从我的 Raspberry Pi 中 运行ning Bittorrent Sync。偶尔它会——出于某种原因——离线。我正在尝试 运行 a script from crontab that will check the connection 但我还想检查 btsync 服务的状态 (sudo service btsync status
)。我怎样才能把它放在一个脚本中,该脚本将从 Crontab 中 运行,查看输出,并在 "running" 以外的情况下启动重启?
检查进程是否为 运行,是否为 ps aux
。将以下脚本命名为 btsync_reboot.sh 并将其 chown
命名为用户 运行 cron.
#!/bin/sh
echo "check service $(date)" >> /var/log/btsync-check.log
ps auxw | grep btsync | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "rebooting at $(date)" >> /var/log/btsync-reboot.log
reboot now >> /var/log/btsync-reboot.log
else
echo "btsync is running" >> /var/log/btsync-check.log
fi
Cron 表达式:* * * * * sh /path/to/btsync-reboot.sh
您可以按照与 checkwifi.sh 相同的步骤操作,但要做到 checkbtsync.sh
按照这些思路应该可行:
#!/bin/sh
btsyncResult=$(sudo service btsync status)
if [[ $btsyncResult != *"is running"* ]]
then
sudo /sbin/shutdown -r now
fi
理论上,这将获取 btsync status 命令的结果并将其作为文本存储在变量中。如果文本不包含单词 'running',它将关闭。其余的就像您提到的 link 中的 checkwifi 步骤:
存储在/usr/local/bin/checkbtsync.sh
然后 运行
sudo chmod 775 /usr/local/bin/checkbtsync.sh
然后 crontab 得到这个新行:
*/5 * * * * /usr/bin/sudo -H /usr/local/bin/checkbtsync.sh >> /dev/null 2>&1
我正在从我的 Raspberry Pi 中 运行ning Bittorrent Sync。偶尔它会——出于某种原因——离线。我正在尝试 运行 a script from crontab that will check the connection 但我还想检查 btsync 服务的状态 (sudo service btsync status
)。我怎样才能把它放在一个脚本中,该脚本将从 Crontab 中 运行,查看输出,并在 "running" 以外的情况下启动重启?
检查进程是否为 运行,是否为 ps aux
。将以下脚本命名为 btsync_reboot.sh 并将其 chown
命名为用户 运行 cron.
#!/bin/sh
echo "check service $(date)" >> /var/log/btsync-check.log
ps auxw | grep btsync | grep -v grep > /dev/null
if [ $? != 0 ]
then
echo "rebooting at $(date)" >> /var/log/btsync-reboot.log
reboot now >> /var/log/btsync-reboot.log
else
echo "btsync is running" >> /var/log/btsync-check.log
fi
Cron 表达式:* * * * * sh /path/to/btsync-reboot.sh
您可以按照与 checkwifi.sh 相同的步骤操作,但要做到 checkbtsync.sh
按照这些思路应该可行:
#!/bin/sh
btsyncResult=$(sudo service btsync status)
if [[ $btsyncResult != *"is running"* ]]
then
sudo /sbin/shutdown -r now
fi
理论上,这将获取 btsync status 命令的结果并将其作为文本存储在变量中。如果文本不包含单词 'running',它将关闭。其余的就像您提到的 link 中的 checkwifi 步骤:
存储在/usr/local/bin/checkbtsync.sh
然后 运行
sudo chmod 775 /usr/local/bin/checkbtsync.sh
然后 crontab 得到这个新行:
*/5 * * * * /usr/bin/sudo -H /usr/local/bin/checkbtsync.sh >> /dev/null 2>&1