Shell Scripting Example 9 – Automatically Restart When Service goes Down

Posted: April 29, 2015 in AutoRestart Production Service, Shell Script

Example 1:

#!/bin/bash
ser=vsftpd
PS=`ps -ef | grep -v grep | grep vsftpd | wc -l`
if [ $PS -gt 0 ]
then
echo “$ser is running”
else
echo “$ser service is not running”
service $ser restart
fi

Example 2

#!/bin/bash
SERVICE=vsftpd

#if ps ax | grep -v grep | grep $SERVICE
if service vsftpd status
then
echo “$SERVICE service running, everything is fine”
else
echo “$SERVICE is not running”
/sbin/service $SERVICE restart
/sbin/chkconfig $SERVICE on

fi

[root@ldap1 ~]# crontab -l
*       *       *       *       *       /bin/sh /root/test1.sh

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s