0

I'm trying to do a comms loss auto restart and just found this script online (bless the person who did it). the script is

ping -c4 www.site.com > /dev/null

if [ $? != 0 ] 
then
  sudo /sbin/shutdown -r now
fi

so this script works perfectly fine. what i wanted to do was add another script with the same function but instead of pinging a site, i wanted to add another so it can ping a server, so what i did was same script but just change what it was pinging

ping -c4 192.168.1.5 > /dev/null

if [ $? != 0 ] 
then
  sudo /sbin/shutdown -r now
fi

i am storing this script on /usr/local/bin/ so the filename on the first script for the comms loss is /usr/local/bin/commsloss.sh and then the filename of the second script is /usr/local/bin/serverloss.sh

i've added both of these lines on crontab -e

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

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

and then i tried turning off the server to see if it will restart but it didn't work.

any suggestions?

i was wondering if this would work or if this is the correct syntax to use so that i can just have one script instead of two?

ping -c4 www.site.com > /dev/null || ping -c4 192.168.1.5 > /dev/null
5
  • Did you make the script execcutable? Jul 25 '17 at 21:48
  • 1
    You might check your system logs for failure messages from the cron jobs. It is inadvisable to use sudo in a cron command line. If you need a cronjob to run with root permissions, add it to a system cron table (e.g., /etc/crontab) as root, or add it to root's personal crontab (sudo crontab -e). Jul 25 '17 at 21:50
  • yes i did @val0x00ff
    – whalien52
    Jul 25 '17 at 21:57
  • i read that if the script needs permission it should be under sudo crontab -e, so removed the script on crontab -e and instead did it on sudo crontab -e and it looks like it worked
    – whalien52
    Jul 25 '17 at 21:58
  • Is /usr/local/bin/serverloss.sh executable? check permision by ls -la /usr/local/bin/serverloss.sh` Jul 26 '17 at 7:26
0

I'm not sure why your current server script isn't working but this should allow you to use the or operator:

#!/bin/bash
ping -q -c4 www.site.com > /dev/null
siteup=$?
ping -q -c4 192.168.1.5 > /dev/null
ipup=$?
if [[ $siteup != "0" || $ipup != "0" ]]; then
  sudo /sbin/shutdown -r now
else
  :
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.