Wednesday, October 1, 2014

Cron to STOP/ START Program

In Linux, you can use crontab to stop/ start applications.

 Following shell script (stop.sh) can be used to grep your program & kill it.

#!/bin/bash

PGID=`ps -eaf | grep YourProgram | grep YourName | grep -v grep | awk '{print $2}'`

if [ -n $PGID ]
then 
/bin/kill -9 $PGID
else
echo "Not Running"

fi

After stopping, you can run the following program(start.sh) to restart the application. This will grep to check whether application runs or not. If runs, shell terminates with "Already Running" error. Else, it will run the application with nohup and send a mail to owner using mailx linux API.

#!/bin/bash

PRGHOME=/home/sujith/ReminderSMS

ps –eaf | grep YourProgram| grep YourName|grep -v grep | awk '{print $2}' > /home/ sujith/.tmp.ReminderAlert

if [ -s /home/ sujith/.tmp.ReminderAlert ]
then

echo "Already Running"

else

#. /home/ sujith/.bash_profile
cd $PRGHOME

echo 'program_alert_started' | mailx sujith@test1.lk
nohup java YourProgram &

rm /home/ sujith/.tmp.ReminderAlert

fi


No comments:

Post a Comment