Inigo Inigo - 3 months ago 12
Python Question

Reboot Raspberry Pi if service not running

I am running Bitorrent Sync from my Raspberry Pi. 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
). 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"?

Answer

You could follow the same steps as you do for checkwifi.sh, but make it 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. 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:

store it at /usr/local/bin/checkbtsync.sh

then run

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

Then crontab gets this new line:

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