Admin Tips 2 : Monitor linux services using bash script

Scenario : –

In one of my resin server,  resin service was crashing due to some resource usage.  It was happening at night time.

I used the following script to monitor the status and restart resin if its not running .

# vi /home/resin/check-resin.sh

#!/bin/sh
run=`ps ax | grep /usr/java/jdk1.6.0_14/bin/java | grep -v grep | cut -c1-5 | paste -s -`
if [ "$run" ];
then
echo "resin is running" > /home/resin/check_resin.log
else
/usr/local/resin/bin/resin-servers.sh restart
mail -s "resin server restarted by check-resin script " admin[at]adminlogs[dot]info < /usr/local/www/hosts/www.adminlogs.info/log/stdout.log
fi

Or the issue is only for a single website ( shared resin hosting ) , you can use the following script and restart the respective server only.

# vi /home/resin/check-resin.sh

#!/bin/sh
cd /tmp
wget www.adminlogs.info:8080
if [ $? -gt 0 ]; then
/usr/local/resin/bin/resin-adminlogs.sh restart
mail -s "adminlogs resin server restarted by check-resin script "  admin[at]adminlogs[dot]info < /usr/local/www/hosts/www.adminlogs.info/log/stdout.log
fi

$? contains the return code of the last executed process. -gt means greater than. Usually programs return zero on success or something else on failure

After  making the following small changes ( use appropriate daemon) you can use the above script to monitor other services like Apache, ftpd,mysql etc as follows :-

For example :-

#  Vi check_httpd.sh

#!/bin/sh
run=`ps ax | grep /usr/local/apache/bin/httpd  | grep -v grep | cut -c1-5 | paste -s -`
if [ “$run” ];
then
echo “apache is running” > /home/admin/check_httpd.log
else
/usr/local/apache/bin/apachectl -k restart
mail -s “Apache server restarted by check-httpd script ” admin [at]adminlogs[dot]info < /usr/local/apache/logs/error.log
fi

Or ( only for apache )

# Vi check_httpd.sh

#!/bin/sh
cd /tmp
wget adminlogs.info:80
if [ $? -gt 0 ]; then
/usr/local/apache/bin/apachectl -k restart
mail -s “Apache server restarted by check-httpd script ” admin [at]adminlogs[dot]info < /usr/local/apache/logs/error.log
fi

Add the script to crontab ( It will check the status in every 5 minutes )

*/5 * * * * /bin/bash check_httpd.sh

Its worked fine  and now I have no worry about that website and getting good sleep 🙂