Friday, August 27, 2010

Watchdog Script for the Linux Processes (Asterisk)

1. Create a script file. Let's say astSvcControl.sh at /root

#!/bin/bash
#script file: astSvcControl.sh

PROCESS="asterisk"

PROCCHK=$(ps aux|grep -c $PROCESS)
if [ $PROCCHK -eq 1 ]
then
/usr/sbin/asterisk
echo "Started Asterisk Service at $(date)" >> /var/log/asterisk/asterisk-check.log
else
echo "$PROCESS is running $PROCCHK processes" >> /var/log/asterisk/asterisk-check.log
fi

[Note: In SuSE linux, ps aux|grep -c < process name > returns 1 if the process is not running and returns 2 or more if the process is running
In SuSE linux, grep
< process name > also returns grep < process name > as one of the process, thus it always returns 1
But some flavors of linux, it returns 0 if the process is not running and returns 1 or more if the process is running
]
2. Make sure that script is executable

#chmod 770 astSvcControl.sh

3. Run the script periodically using cronjobs

#crontab -e
* * * * * bash /root/astSvcControl.sh

(run every minute)


If you want to run the script every 5 mintutes , do this

#crontab -e
*/5 * * * * bash /root/astSvcControl.sh



More on cronjobs for Linux/Unix:
http://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command/
http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/

No comments:

Post a Comment