I have one process which runs 24*7 daily and I always has to monitor it now i want to have this automated such that if it stops at anytime we get a exception mail.
For example i use ps -ef|grep 'etl'
above command gives me the desired output.
What i am thinking is like to run this script using cron which will run in evry 5 minutes.
So i need a validation script which works like if my process stops it will throw an exception via mail and will be able to start it immediately.
#!/bin/bash
#Get the number of processes containing etl running
VAR=`ps -ef | grep 'etl' | grep -v grep | wc -l`
#Verify if VAR >= 1
if [ $VAR -lt 1 ]; then
echo "Problem - No etl process running" | mail -s "No process Alert" email@address.com
fi
#END
Well it is quite a simple script. But i'm also a begineer and I will explain it for you:
First we declare a variable VAR that computes the number of processes running from the process listing( ps -ef ) that includes "etl" in their name (by using command substitution on bash ` .. `)
Here we use grep -v grep to exclude the search itsel since a new process will be spawned for grep 'etl' and we don't want this to be counted.
Then we count the remaining lines by using wc -l
At this point our VAR variable will hold the number of processes running.
We check if the number of process obtained is lower than 1...basically meaning that is 0 which tell us that no process is running.
If this is true than we send an email, if not we do nothing.