Help with script, shutdown if no ping responce

Hi, i would like to create a script that shuts down the system if the power fails, basicly the solaris box would load the script at startup.

Ping 192.168.1.100 every 20 secs
If cannot get responce 3 times in a row send init 0 to the system.

Simple but effective, as 192.168.100.1 wont be on the ups backup, so the solaris system will know the power is down and it will shutdown.

Im sure it wont be hard to do im just new to unix, could someone help me with this?

anyone? :frowning:

Erm........

If I am reading this right.....you want to send a shutdown command to a machine, the power to which, has failed. If the power has failed the machine will be dead in the water and not be able to respond to *any* commands.

Have you described your requirement clearly and accurately as at present, unless I have badly misread it, it doesn't make a whole hep of sense

I have a unix machine which is on a UPS with only about 15mins battery life, what i would like to happen is the unix machine shutdown safely if the power fails. The way i want to do it, would have the unix machine ping my cable modem every 20 secs, which is not on the UPS, so if the ping was to have no responce the modem would probably be off, which means the power would have failed.

I would like the unix machine to shutdown if it cant contact the cable modem 3 times in a row via ping.

#!/bin/sh

while true
do
      sleep 300
      REBOOT=/usr/bin/true
      for d in a b c
      do
            if /usr/sbin/ping cable-ip
            then
                 REBOOT=/usr/bin/false
                 break
            fi
      done
      if $REBOOT
      then
             shutdown -g 1 -y -i 5
      fi
done

or better still, remove outer while loop and install as cron job.

Note, Solaris "ping" works quite differently to other platform pings.

Thanks! would this work with solaris 10?

Give it a go.... :slight_smile:

Make sure you understand it all before you install it, as it has to run as root to run shutdown.

Il tell you the truth i dont really understand it :slight_smile:

Yeah i can sort the root thing out i hope, how would i go abouts installing it?

No, I'm just trying to alert you to an obvious difference in behaviour between Solaris and other platforms.

Normal ping is continuous and emits a trace of packet echo times, Solaris just says "hostname is alive". The important part is it returns an exit code we can test.

Thankyou!

Would the script run every so many seconds by itself? Sorry about all the Q's iv been using solaris about 3 days.

the sleep 300 is part of a loop to sleep for five minutes each loop.

However I recommend you remove the outer loop and install as a cron job.

Search this site for "crontab" for examples.

If you are installing this as root then I expect you to have a certain level of understanding to know exactly what it is you are installing.

If you are not sure, then do some testing with crontab under a normal user account.

I know how to setup file systems, samba, perform basic system administration but im very rusty on the scripting side of things.

could i ask what would be the diffrence if i installed it as a cron job?

Edit: doesnt matter, i understand if i set it to run as a cron job, it will just run the whole script through every time, but would the system shutdown on the first failed ping?

Yes, you don't have the outer while loop with the sleep.

Is this the correct way to setup the crontab, for the script to run every min?

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60     *     *     *     *     /rhys/ping

or

1    *     *     *     *     /rhys/ping

I suggest you test crontab under a normal user account to get a feel for it.

Ok thanks, il do that now, in the script you provided do i replace cable-ip with the ip of my modem?

Yes, I do encourage you to understand the script line by line. Of course you may have the IP address in /etc/hosts to assist, rather than coding into the script. So if the address changes you only have to change it in one place.

To test the script i just put the ip address straight in.

I have defined the script in the cron file to run every min as root in vmware.

If i pull the wire on the modem and leave for 5 mins the machine will still be on fine.

I cant get this to work. Could you rewrite it quickly for me so it is ready to be installed as cron pinging 192.168.100.1?

#!/bin/sh

DOSHUTDOWN=/usr/bin/true

for d in a b c
do
    if /usr/sbin/ping cable-ip
    then
         DOSHUTDOWN=/usr/bin/false
         break
    fi
done

if $DOSHUTDOWN
then
   shutdown -g 1 -y -i 5
fi

I suggest you just save this script, make it executable then test it as root *before* installing it as a cronjob.

Also, I suggest you add some kind of test at the start to only enable the shutdown after a completely successful bootup, otherwise you may get in the situation where as soon as you start the box up, it starts cron, and finds it has no path to the modem and shuts down before getting the network started.