Modifying the Restart Script

Hello,

I have a shell script which calls for an existing script with appropriate parameters. It looks something like this.

--------------------------------------------------------------------------

#!/bin/bash
sh /root/ams_rc stop_shepherd > /dev/null
sleep 30
sh /root/ams_rc start_shepherd > /dev/null
exit 0

-------------------------------------------------------------------------
This script works fine. It stops and then starts the process.
I want to modify this script, with fail safes in it.

The script should check to see either if process has stopped properly on stop command or if process has started properly on start command.

Any help would be appreciated.

Thanks,
Siddhesh.K

#!/bin/bash
sh /root/ams_rc stop_shepherd > /dev/null
if [ "$?" eq "0" ]
then
 sleep 30
 sh /root/ams_rc start_shepherd > /dev/null
else
 echo "Not stopped Properly"
 exit 1
fi
exit 0

Hi,

Thanks for the reply.

I am not sure what exactly this means. Could you please elaborate the IF condition.

Regards,
Siddhesh.K

A special variable called $? holds the last executed command exit status

in the if condition, we are checking the status. 0(zero) means success.

if it is success, then we are triggering the start script. Otherwise exit the shell script with exit status 1

---------- Post updated at 11:38 AM ---------- Previous update was at 11:38 AM ----------

Unix - Special Variables

Thanks again,

Can we use the following logic.
----------------------------------------------------------
Stop Shepherd
sleep 30
loop : IF (Stop Shepherd = "Shepherd not running")
then Start Shepherd
else loop 3 times ( If 3 attempts fail ... Alarm/email )
wait 30

loop2: IF (ps -ef | grep shep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}' | wc -l = 3 )
then echo shepherd started success
else loop2 3 times ( If 3 attempts fail ... Alarm/email )
----------------------------------------
To elaborate more on this,

  1. It will stop the process.
  2. Sleep for 30 seconds
  3. then i want to put the same in loop to see if the process has been stopped.
  4. if the condition in 3 satisfies then it will start the shepherd.
  5. Else will loop in for stopping process for 3 times.

Can this be written in script.
Thanks in advance

Regards,
Siddhesh.K

In this case and as well as in the previous script, why are you exectuting sleep 30 secs. The moment

sh /root/ams_rc stop_shepherd > /dev/null

command returns, shepherd should have been stopped.

How could you put a loop and check the status of already stopped to "not running" status. It will be true in the first iteration itself.

Please post your requirement with out any code in mind. That will help us understand what you are looking for.

Hi,

Sometime it may happen that the shepherd process may not stop. so be sure it is stops i was willing to put a loop to check the same, then start the shepherd process

Siddhesh.K

---------- Post updated at 12:40 PM ---------- Previous update was at 12:39 PM ----------

The original file looks like this.

stop_shepherd() {
        . $PROFILE
    echo "Stopping shepherd processes"
    PID=`cat $SERVER_HOME/alerts/logs/shepherd.pid`
    ps -ef | grep shep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}' > $SERVER_HOME/tmp/shepherd.pids
    if [ -s $SERVER_HOME/tmp/shepherd.pids ]
    then
        cat $SERVER_HOME/tmp/shepherd.pids | xargs kill -9
        echo "Shepherd stopped"
        rm -f $SERVER_HOME/tmp/shepherd.pids
    else
        echo "Shepherd not running"
    fi
    }

start_shepherd() {
        . $PROFILE
        echo "Starting shepherd processes"
        ps -ef | grep shep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}' > $SERVER_HOME/tmp/shepherd.pids
        if [ -s $SERVER_HOME/tmp/shepherd.pids ]
        then
        echo "Shepherd already running"
                rm -f $SERVER_HOME/tmp/shepherd.pids
        else
            cd $SERVER_HOME/alerts/bin
            su - shep-1 ./shepherd.sh
        rm -f $SERVER_HOME/tmp/shepherd.pids
        echo "Shepherd started"
        fi
    }

If you look at you stop_shepherd function, it doesn't execute any background commands, so there are only two possible cases here,

1.if everything goes well, your process will be killed.
2. shepherd wasn't running at all.

After any one of this case has happened,only then your command will return . Now tell us how will shepherd process may hang without stopping. Your are stopping the process by kill -9, I don't think it will ever hang.

stop_shepherd() {
        . $PROFILE
    echo "Stopping shepherd processes"
    PID=`cat $SERVER_HOME/alerts/logs/shepherd.pid`
    ps -ef | grep shep-1 | grep -v grep | grep -v wls100ctl | awk '{print $2}' > $SERVER_HOME/tmp/shepherd.pids
    if [ -s $SERVER_HOME/tmp/shepherd.pids ]
    then
        cat $SERVER_HOME/tmp/shepherd.pids | xargs kill -9
        echo "Shepherd stopped"
        rm -f $SERVER_HOME/tmp/shepherd.pids
    else
        echo "Shepherd not running"
    fi
    }

Ok. Thanks

So this simple code as mentioned earlier will meet my purpose. right?

#!/bin/bash
sh /root/ams_rc stop_shepherd > /dev/null
if [ "$?" eq "0" ]
then
 sleep 30
 sh /root/ams_rc start_shepherd > /dev/null
else
 echo "Not stopped Properly"
 exit 1
fi
exit 0

Siddhesh.K

Yes, if you still face any problems as such if it isn't stopping then post that scenario, we can check further.