Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Monitor a process and restart if fails 1

Status
Not open for further replies.

seowsheng

IS-IT--Management
Mar 26, 2003
2
SG
Hello,

I need help to write a script that monitor my application server.pid. If fails then the script should autorestart the process. I am thinking to put this script in cron job.

Thanks.
 
Your script could be something as simple as it executes a ps, counts how many times your program appears, then runs a command if it does not appear in the ps listing. I would do it like this:

All you need to do is copy and paste the following lines into a blank file and run it on cron as often as you want.

#!/usr/bin/ksh

# Written by: Bryan Pinos for a memeber of Tek Tips
# Date: March 26, 2003

# This program should be named entirely different than what
# you are searching for since it will be running in memory
# and could affect what it finds running in memorry.

# Place some sort of criteria in the variable pidname that
# can uniquely identify the pid command you want monitored.

pidname="httpd"
numexists=`ps -ef | grep -c $pidname`

# We must seacrh for greater than one since the grep command
# will contain what we are searching for.

if [ $numexists -gt 1 ]; then
echo "ALL IS WELL WITH: $pidname"
else
echo "*******Program needs restarted!!!********"
echo "Executing command"
# Place command to start your program here
fi
 
I really think we need FAQ on this as it has been asked umpteen times.

Any volunteers or should I dive in?
 
Being AIX, I'd look into adding the thing to the src* functions, let it cope with monitoring and restarting. Or maybe I'm thinking inittab....
 
A while ago, I developed a similar script to bpinos above. However, I found the method of determining whether the process is still running (by using `ps|grep` to be unsatisfactory. Instead, the process id is stored in a file at launch time and this is used by subsequent status checks.
[tt]
#----script to launch a job and keep it running

EXEC_JOB="sleep 20" #----just an example
PID_FILE="/var/home/pid_file_sleep" #----or whatever

ps -p `cat $PID_FILE 2>&1` >/dev/null 2>&1
if [ $? -ne 0 ]
then
echo launch executable in background
$EXEC_JOB &
echo $! > $PID_FILE #----capture process id
else
echo process is still running ok
fi
 
Or instead of ps -p, you can use kill -0
Code:
kill -0 $(cat $PID_FILE) 2>/dev/null
if [ $? -ne 0 ]
then
  ## do something
else
  echo process still running
fi
Cheers, Neil
 
Is 'inittab' a linux-only feature?

Else you could insert a line:

seos:45:respawn:/usr/local/bin/seowsheng_scrip.sh

which is:
identifiere (uniq combination of 1-4 characters)
runlevel (multiuser+nework, X11)
action (respawn: restart, whenever it terminates)
process (your script)

man inittab should answer more questions.

seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top