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.
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.
# 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
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
which is:
identifiere (uniq combination of 1-4 characters)
runlevel (multiuser+nework, X11)
action (respawn: restart, whenever it terminates)
process (your script)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.