Shell script to monitor process status
Here is a simple Shell script to monitor the status of a particular process running on the server. By using this script as scheduled Cron job we can monitor any process running on the server and through this, we can avoid critical service failures.
What the script actually doing is that this will monitor the process at a regular interval and automatically start the same if it is not running, finally send a notification mail to our mailbox with a custom alert message that we configured in our script.
There are lots of scripts available to monitor the status of every process but this is the one that I used on my server.
ps_status=`ps -ef | grep -v grep | grep $service | wc -l`
if [ “$ps_status” -gt “0” ] then
echo “$service is running!!!” >> /dev/null 2>&1
echo -e “Date: `date ‘+%d/%m/%Y_%H:%M:%S’`” >> “$log_file” 2>&1
/etc/init.d/”$service” start >> “$log_file” 2>&1
echo -e “Subject:Alert – Apache process down on `hostname`\n\nHello,\n\nApache process restarted, please verify the application functionality\n\nRegards,\n” | /usr/sbin/sendmail “$recipients” >> “$log_file” 2>&1
recipients: In the recipients section, you can use the mail address to which you need to send the notification. If you want to notify multiple users, then enter those additional email addresses in the recipients field separated by a space.
ps_status: The ps_status variable stores the total count of Apache processes running on the server and pass the same to if loop.
The mail subject and other alert information’s are included in the last field. You can replace the subject and contents with your own.
I have tested this script before posting on this blog and it is currently running on one of my server 🙂
Hope this script will be useful for you !!!