Executing Bash Script by help of cron scheduler

Taking care of your Linux box.

Executing Bash Script by help of cron scheduler

Postby syed » Fri Aug 06, 2010 10:31 am

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
syed
Subedar Major
 
Posts: 439
Joined: Thu Jul 28, 2005 3:51 pm

Postby mudasir » Sat Aug 07, 2010 10:44 am

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
mudasir
Captain
 
Posts: 565
Joined: Tue Oct 17, 2006 5:23 am
Website: http://www.crystalnetworks.org
Location: Dubai

Postby lambda » Tue Aug 10, 2010 11:06 am

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"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby syed » Fri Aug 27, 2010 11:21 am

Salam 2 all

thanks for reply. Then where should it be placed.



Regards


Raza
syed
Subedar Major
 
Posts: 439
Joined: Thu Jul 28, 2005 3:51 pm

Postby lambda » Sun Aug 29, 2010 2:48 pm

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"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Postby ranatanveer » Thu Sep 30, 2010 1:34 am

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
ranatanveer
Subedar
 
Posts: 355
Joined: Sat May 07, 2005 11:54 am
ICQ: 133032001
Website: http://www.affordableprogrammers.com
WLM: ranatanveer@gmail.com
Yahoo Messenger: ranahard@yahoo.com
Location: Lahore


Return to “%s” Administration

Who is online

Users browsing this forum: No registered users and 1 guest

cron