LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Setting up cron to proccess a Bash Script to run every 15 minutes (https://www.linuxquestions.org/questions/programming-9/setting-up-cron-to-proccess-a-bash-script-to-run-every-15-minutes-572070/)

jamtech 07-25-2007 12:41 AM

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========================================

wjevans_7d1@yahoo.co 07-25-2007 12:52 AM

What is your question?

Does everything work? If not, what doesn't work, and exactly what does it do instead?

jamtech 07-30-2007 01:51 PM

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

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

colucix 07-30-2007 03:32 PM

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


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