How to run a script on shut down

Erik Christiansen dvalin at internode.on.net
Thu Nov 27 07:38:50 UTC 2008


On Wed, Nov 26, 2008 at 07:56:51PM -0800, Ray Parrish wrote:
> Thanks Erik.  I've got the script working pretty good without using 
> cron. My site was just down for about 15 minutes, and my script told me 
> when it went down, and when it came back up. It was down for about five 
> minutes earlier today too, so I'm going to get on my web host to find 
> out why it keeps going down. [it's a windows server is why] 8-)

That's great. It is fun to write your own scripts, when time allows, and
it provides experience that neither book nor manpage can.

Just to show that crontabs only _look_ scary, here's a quick example,
where I just use it to ping another host:

$ cat /home/erik/bin/host_check
                          -------------------
#!/bin/bash

ping -q -c 1 $1 &> /dev/null || \
   xmessage -nearmouse -bg red -fg yellow "$1 is down" -display :0.0

                          -------------------

$ crontab -e      # Then add this one line. (Default editor may be vim):

*/3 * * * * /home/erik/bin/host_check 192.168.1.250

That's the simple periodic host_check thing done in two lines, and
the cup of coffee's not yet cold. :-)

This example pings once every 3 minutes. For every minute, delete the
"/3".

It's tested. (The dialogue popped up twice while typing this, because
that IP is one that doesn't exist.) The redirection of stdout & stderror
to /dev/null was only necessary because -q isn't genuinely quiet.

Anyway, it's just FYI, should some other periodic thingy walk across
your path.

Erik

-- 
To be or not to be. -- Shakespeare
To do is to be.     -- Nietzsche
To be is to do.     -- Sartre
Do be do be do.     -- Sinatra





More information about the ubuntu-users mailing list