LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-25-2007, 01:41 AM   #1
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Rep: Reputation: 15
Setting up cron to proccess a Bash Script to run every 15 minutes


I would like to set up cron to proccess a Bash Script to run every 15 minutes.

The script is listed below for your review and modification:
Code Start========================================
#!/bin/bash
# Simple SHELL script for Linux and UNIX systems that will monitor the
# hosts with the ping command and email failed ping requests to your email address
# -------------------------------------------------------------------------
# add ipaddress or hostnames that will be monitored
# hostname and ipaddress are separated by a space
HOSTS="192.168.1.1 24.234.0.71 10.1.0.1"

# number of ping request
COUNT=1

# email report when a host does not reply back
SUBJECT="A host is Down!"
EMAILID="admin@localhost.com"
for myHost in $HOSTS
do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
# 100% failed
echo "Host : $myHost is down (There was no reply from this host) at $(date)" | mail -s "$SUBJECT" $EMAILID
fi
done
Code End========================================
 
Old 07-25-2007, 01:52 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
What is your question?

Does everything work? If not, what doesn't work, and exactly what does it do instead?
 
Old 07-30-2007, 02:51 PM   #3
jamtech
LQ Newbie
 
Registered: May 2007
Distribution: Suse 10 and 11
Posts: 25

Original Poster
Rep: Reputation: 15
The Script works fine.

I would like to stream line it so that the script reads from a text file instead of reading from this line that is coded within the script.

# add ipaddress or hostnames that will be monitored
# hostname and ipaddress are separated by a space
HOSTS="192.168.1.1 24.234.0.71 10.1.0.1"


As you can see this line within the script would be and can become very long and unmanageable and I would run the risk of causing damage to the script by having to add or remove a hosts ip address.

I would like for the script to read from a text file instead of the above line.

I am trying to set the script up to run every 15min. via a cron job. I placed the script in the bin folder and setup all the correct rights for the script to be runned from any term window.

The results of the script is listed below for your review.

------------------------------------------------------
Subject: A host is Down!
Date: Wednesday 25 July 2007 21:10
From: "Ghost" <ghost@ghost.com>
To: admin@ghost.com

Host : 10.1.0.10 is down (There was no reply from this host) at Wed Jul 25
*21:10:54 PDT 2007

------------------------------------------------------
 
Old 07-30-2007, 04:32 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
I would like to stream line it so that the script reads from a text file instead of reading from this line that is coded within the script.
This task can be achieved by the following construct:
Code:
for myHost in `cat hostlist`
do
   <commands here>
done
where hostlist can be a file containing IP addresses, one per line.

Quote:
I am trying to set the script up to run every 15min. via a cron job. I placed the script in the bin folder and setup all the correct rights for the script to be runned from any term window.
See "man -S5 crontab" for details. I think you need to setup something like the following crontab entry:
Code:
0,15,30,45 * * * * $HOME/bin/myScript.sh
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run bash commands script as cron anjanesh Linux - General 4 04-03-2007 07:25 AM
Run a script every 10 minutes hraposo Ubuntu 3 11-08-2006 07:44 AM
execute a script every 5 minutes "cron" ALInux Linux - General 1 02-10-2006 03:36 AM
howto run a script from cron every 5 minutes except 11pm-8am ? cccc Programming 2 03-20-2005 04:52 AM
bash script from cron kubla Programming 3 01-22-2004 05:30 AM


All times are GMT -5. The time now is 08:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration