Batch script to monitor service
Posted on 2012-09-10
I try to do some monitoring of a few services and see that the are running and if they are not have them restarted or started. I have a base as below bit it just does not want to work. In addition I try to expand it to add a few more services.
Basically I want to monitor amavisd, postfix, dovecot, spamassassin, httpd and mysql
If any of them is down I want to try to restart up to 3 times and if there is a issue starting them send a email.
Currently I have the following:
#checking if service is running
ps -e | grep $service > /dev/null
#if service not running lets try restart 3 times
if [ "$servicestat" != 0 ]; then
while [ "$count" -le "$threshold" ]
#attempt to start the service
/etc/init.d/$service start >> $servicelog 2>&1
if [ $? != 0 ]; then
exit 0 #if service started exit
#if service could not be started, notify
cat $servicelog | mail -s "problem starting $service" $email 2>/dev/null
If anyone could assist me to at least find out what is wrong here. I run a RHES 6 64bit. It does not give any error but does not start the service when I shit it down.