Results 1 to 3 of 3
Hey guys, I'm trying to create a bash script that will email once when a machine goes down and once when it comes back up. If it stays up I ...
- 01-24-2012 #1Just Joined!
- Join Date
- Sep 2009
- Posts
- 2
Bash script to email once when server down and once when server up
Hey guys, I'm trying to create a bash script that will email once when a machine goes down and once when it comes back up. If it stays up I want it to do nothing. I only want to get one email for each instance. I was thinking of setting it to run in cron, but I dont want multiple instances of the script to run. Maybe I can set a lock file?
Here is the script so far:
I'm trying to figure out how to setup the lockfile to work properly so I've been just printing "machine up" or "machine down" for testing and will need to replace it with the email functions once it's ready. I would appreciate any input.Code:#!/bin/bash if [ -e monitor.txt ]; then echo "File exists" else echo "File does not exists. Create file now." echo 1 > monitor.txt fi lockfile script.lock a=$(/usr/bin/tail -n 1 monitor.txt) if [ $a -gt 1 ]; then a=$(/usr/bin/tail -n 1 monitor.txt) /usr/bin/expr $a - 1 >> monitor.txt fi if [ $a -lt 2 ]; then a=$(/usr/bin/tail -n 1 monitor.txt) /usr/bin/expr $a + 1 >> monitor.txt fi rm -f script.lock && # add ip / hostname separated by white space HOSTS="10.1.0.12" # no ping request COUNT=3 for myHost in $HOSTS do count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }') if [ $count -eq 3 ]; then exit # Machine is Down else echo "Machine is Down" fi done
Thanks!
- 01-24-2012 #2
This may be a little OT, but imho there is no need to reinvent the wheel.
Existing monitoring solutions such as Incinga , Nagios, Xymon and others already have such features. And many more.
So e.g. xymon will alert failure and recover per host, as per your requirement.
It also features a "repeat" keyword: You will get another alert after X time.
For example:
- The hosts get probed every 5min.
- One host fails -> alert
- Repeat is set to e.g. 30min.
- So you will get the next alert only after 30min, not on the next regular 5min run
This is nice to not forget failed hosts/services.
Configuring Xymon AlertsYou must always face the curtain with a bow.
- 01-25-2012 #3Linux Guru
- Join Date
- May 2011
- Posts
- 1,843
I agree with Irithori entirely. I also use Xymon and highly recommend it.
As to your question about making sure a program is not running duplicates, I usually do this use Perl instead, as it has modules built into it that allow such exclusivity. That is probably not what you want to hear, though. When I've had such a need before in Bash, I'd typically just look for an instance of the same process name already running in ps, e.g.:
Note the special $$ variable above is a bash shell built-in variable that refers to the pid of the current shell (i.e., ignore the pid of this script b/c we're looking for other instances of the script).Code:#!/bin/bash pids=$(pidof -x $0 -o $$) if [ -n "$pids" ]; then echo "Another instance of $0 (pid $pids) is already running" exit 1 fi
I don't like trusting lock files, they have ways of not cleaning up after themselves...


Reply With Quote