Inigo Inigo - 1 year ago 93
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 Source

You could follow the same steps as you do for, but make it

Something along these lines should work:

btsyncResult=$(sudo service btsync status)

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

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/

then run

sudo chmod 775 /usr/local/bin/

Then crontab gets this new line:

*/5 * * * * /usr/bin/sudo -H /usr/local/bin/ >> /dev/null 2>&1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download