Executing Bash Script by help of cron scheduler

Taking care of your Linux box.
Post Reply
syed
Subedar Major
Posts: 439
Joined: Thu Jul 28, 2005 3:51 pm

Executing Bash Script by help of cron scheduler

Post by syed »

Dear salam to all

I want to excute the following script with help of cron scheduler every morning at 9.00 am. Can any body help me?


#!/bin/bash

LOG=/tmp/mylog.log
SECONDS=10
EMAIL=my@email.address

for i in $@; do
echo "$i-UP!" > $i.$LOG
done

while true; do
for i in $@; do
ping -c 1 $i &> /dev/null
if [ $? -ne 0 ]; then
STATUS=`cat $i.$LOG`
if [ $STATUS != "$i-DOWN!" ]; then
echo "`date`: ping failed, $i host is down!" |
mail -s "$i host is down!" $EMAIL
fi
echo "$i-DOWN!" > $i.$LOG
else
STATUS=`cat $i.$LOG`
if [ $STATUS != "$i-UP!" ]; then
echo "`date`: ping OK, $i host is up!" |
mail -s "$i host is up!" $EMAIL
fi
echo "$i-UP!" > $i.$LOG
fi
done
sleep $SECONDS
done


Regards,

SM Raza
mudasir
Captain
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Location: Dubai
Contact:

Post by mudasir »

Its simple.

run this command "crontab -e" then make an entry similar to given below

* 9 * * * /path/to/script

then save and exit. make sure the script has executable rights.
Kind Regards
Mudasir Mirza (RHCE)
(+971)55-1045754
http://www.crystalnetworks.org
http://www.diglinux.com
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

that will execute 60 times, once a minute from 9:00 to 9:59.

0 9 * * * is what you want.

it doesn't seem like the script will ever exit. perhaps cron is not the place for it.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
syed
Subedar Major
Posts: 439
Joined: Thu Jul 28, 2005 3:51 pm

Post by syed »

Salam 2 all

thanks for reply. Then where should it be placed.



Regards


Raza
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

remove the outer while loop so that it does exit after a 10 seconds.

install runit or daemontools (probably part of your distribution), and run it using that.
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
ranatanveer
Subedar
Posts: 355
Joined: Sat May 07, 2005 11:54 am
Location: Lahore
Contact:

Post by ranatanveer »

AOA SM Raza

one suggestion:

what if mail delivered in case of only host down ?

and you can configure mail-to-sms from any service provider in pakistan so that you get mail at ur mobile as SMS.
Regards

Rana Tanveer
+923224194457
Linux Student

For Affordable Web Development http://www.affordableprogrammers.com
http://www.qualityprogrammers.com
Post Reply