简体   繁体   English

如果服务未运行,请重新启动Raspberry Pi

[英]Reboot Raspberry Pi if service not running

I am running Bitorrent Sync from my Raspberry Pi. 我正在从Raspberry Pi运行Bitorrent Sync。 Very occasionally it will- for some reason- go offline. 由于某种原因,它偶尔会脱机。 I am trying to run a script from crontab that will check the connection but I also want to check the status of the btsync service ( sudo service btsync status ). 我正在尝试从crontab运行脚本,该脚本将检查连接,但我也想检查btsync服务的状态( sudo service btsync status )。 How can I put this in a script that will run from Crontab, look at the output, and initiate a reboot if anything other than "running"? 如何将其放在将从Crontab运行的脚本中,查看输出,并在“运行”以外的情况下重新启动?

You could follow the same steps as you do for checkwifi.sh, but make it checkbtsync.sh 您可以按照与对checkwifi.sh相同的步骤进行操作,但是将其设置为checkbtsync.sh

Something along these lines should work: 遵循以下原则应该可以:

#!/bin/sh    
btsyncResult=$(sudo service btsync status)

if [[ $btsyncResult != *"is running"* ]]
then
    sudo /sbin/shutdown -r now
fi 

Theoretically, that will take the result of your btsync status command and store it in the variable as text. 从理论上讲,它将采用btsync status命令的结果并将其存储为文本形式的变量。 if the text doesn't contain the word 'running' it shuts down. 如果文本中不包含“正在运行”一词,它将关闭。 The rest is just like the checkwifi steps at the link you mentioned: 其余的就像您提到的链接中的checkwifi步骤一样:

store it at /usr/local/bin/checkbtsync.sh 将其存储在/usr/local/bin/checkbtsync.sh

then run 然后跑

sudo chmod 775 /usr/local/bin/checkbtsync.sh

Then crontab gets this new line: 然后,crontab会得到以下新行:

*/5 * * * * /usr/bin/sudo -H /usr/local/bin/checkbtsync.sh >> /dev/null 2>&1

Check if process is running, with ps aux . 使用ps aux检查进程是否正在运行。 Name the below script as btsync_reboot.sh and chown it to user running cron. 命名以下脚本btsync_reboot.shchown它来运行用户的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 expression: * * * * * sh /path/to/btsync-reboot.sh Cron表达式: * * * * * sh /path/to/btsync-reboot.sh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM