Operating System - HP-UX
1820238 Members
2646 Online
109620 Solutions
New Discussion юеВ

check me for a process and restarting if the process is not found

 
SOLVED
Go to solution
Deoncia Grayson_1
Honored Contributor

check me for a process and restarting if the process is not found

hello everyone

I relatively new to the scripting world and would like a script that would go out and check a process and if it doesn't find it, the restart it. The process is called rclproc, any help would be appreciated.
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
16 REPLIES 16
Scott Palmer_1
Trusted Contributor

Re: check me for a process and restarting if the process is not found

Deoncia,

there are many ways to do this

this example is one that will start a program that will run for ever and check ever 5 minutes to see if the program is running. If not it restarts the program.

#! /bin/sh

while true; do # infinite loop
var=$(ps -ef | grep -v grep | grep rclproc) # check to see if program is running

if [[ -z "$var" ]]; then
/usr/local/bin/rclproc > /dev/null 2>&1 & # run program if nothing is found inthe ps
else
var="" # reset the variable to null
fi
sleep 300 # sleep 5 minutes
done


you could always eliminate the infinate loop, and run it from cron ever 5 mintues or so

Hope this is helpfull
Sincerely

--Scott Palmer
Scott Palmer_1
Trusted Contributor

Re: check me for a process and restarting if the process is not found

to start the program

create a script foo2 with the code snippet in it.

chmod 750 foo2
./foo2 > /dev/null 2>&1 & # run pipe output to /dev/null and background process.

or you could do
echo "./foo2" | at now

etc.

Sincerely

--Scott Palmer
Sundar_7
Honored Contributor

Re: check me for a process and restarting if the process is not found

Deoncia,

You can try something like this

#!/usr/bin/sh
PROCNAME="rclproc"
while true
do
ps -ef | grep -q "$PROCNAME" >/dev/null 2>&1
if [[ $? -ne 0 ]]
then
# Process is not there
/path/to/the/procname
[ $? -eq 0 ] && echo "$PROCNAME successfully started"
fi
sleep 300
done

- Sundar
Learn What to do ,How to do and more importantly When to do ?
Sridhar Bhaskarla
Honored Contributor
Solution

Re: check me for a process and restarting if the process is not found

Hi,

This script does little bit of checking. Modify the lines with appropriate details whereever I indicated them with # mark before running. It basically does the following

1. check if the process is running. If so, do nothing.
2. If the process is not running, then log a message in /tmp/rclproc.log file and start it. If it starts, then log a successful message into the log file.
3. If the startup was not successful, then send you a mail and creates file /tmp/rclproc.nocheck and will stop monitoring the process. Fix the errors, remove the stamp file and it will start monitoring it again.

Put it in 'cron' to run every 5 mins. Test it prior to implementing it.

#!/usr/bin/ksh

PROC="rclproc"
STARTCMD="your_command_to_start_rclproc" #<==modify
LOGFILE=/tmp/rclproc.log
EMAIL=your_mailid@yourdomain.com #<==Modify
STAMP=/tmp/rclproc.nocheck
touch $LOGFILE

check_pid()
{
MESG=$1
STATUS=$2

UNIX95= ps -C "$PROC" > /dev/null 2>&1
if [ $? != 0 ]
then
echo "$(date): $PROC $MESG" >> $LOGFILE
$STARTCMD
else
echo "$(date): $PROC has been successfully started" >> $LOGFILE
STATUS=0
fi

}

if [ ! -f $STAMP ]
then
check_pid "not running" 0
check_pid "is still not running" 1

if [ $STATUS = 1 ]
then
echo "Unable to start $PROC. Fix the errors
and remove $STAMP file to resume monitoring"| mailx -s "$PROC failure" $EMAIL
touch /tmp/envd.nocheck
fi
fi



-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Rodney Hills
Honored Contributor

Re: check me for a process and restarting if the process is not found

The /etc/inittab file (do man inittab) has entries to start scripts at boot time.

One of the options is "respawn" which will start a script at boot, and if it terminates, will relaunch it.

HTH

-- Rod Hills
There be dragons...
Deoncia Grayson_1
Honored Contributor

Re: check me for a process and restarting if the process is not found

Hey Sri,


Thanks for the script but your script doesn't really just stops at checking for the process it goes out and restart it as well.
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sridhar Bhaskarla
Honored Contributor

Re: check me for a process and restarting if the process is not found

Yes. It does and I believe that's your primary requirement. If it couldn't start your program successfully, then it will send you a mail and then will stop monitoring until you fix the problem and remove the stamp file. This is one of the simple logics I use for in my serviceguard scripts.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Cheryl Griffin
Honored Contributor

Re: check me for a process and restarting if the process is not found

This is a common question.
Be sure to check out the answer here as well:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=199874

There might be some other alternatives as well.
"Downtime is a Crime."
RAC_1
Honored Contributor

Re: check me for a process and restarting if the process is not found

kill -0 $(ps -ef|grep [r]clproc|awk '{print $2}' || "start_process"

"start_process" refers to starting the process. If it is being started from /sbin/init.d/rclproc, then the above command look like follows.

kill -0 $(ps -ef|grep [r]clproc|awk '{print $2}' || /sbin/init.d/rclproc start

Anil
There is no substitute to HARDWORK
Fred Ruffet
Honored Contributor

Re: check me for a process and restarting if the process is not found

You should consider running this program as respawn in /etc/inittab. Such programs are automaticaly re-run when they end (kill or normal end).

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Deoncia Grayson_1
Honored Contributor

Re: check me for a process and restarting if the process is not found

Hey Sri,

I think you misunderstood me, I don't want it to restart the process, if the process is already running, if the process is still running then it should just sleep and if its not found then restart it.
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Sundar_7
Honored Contributor

Re: check me for a process and restarting if the process is not found

Deoncica,

"I don't want it to restart the process, if the process is already running, if the process is still running then it should just sleep and if its not found then restart it. "

The above description is confusing for sure, atleast to me. :-)

- Sundar.
Learn What to do ,How to do and more importantly When to do ?
Sundar_7
Honored Contributor

Re: check me for a process and restarting if the process is not found

OK, I think I understand what you meant.

Sri script is meant to be executed from cron. Schedule that script to run once in every N minutes via cron.

If you dont want to do that, you can use the script excerpt from my post, which uses an infinite loop with sleep command.
Learn What to do ,How to do and more importantly When to do ?
Sridhar Bhaskarla
Honored Contributor

Re: check me for a process and restarting if the process is not found

Hi,

I guess I know what you meant. It doesn't really restart the process but it only displays the message. Replace the function check_pid like this

check_pid()
{
MESG=$1
STATUS=$2

UNIX95= ps -C "$PROC" > /dev/null 2>&1
if [ $? != 0 ]
then
echo "$(date): $PROC $MESG" >> $LOGFILE
$STARTCMD
else
STATUS=0
fi

}
You may be disappointed if you fail, but you are doomed if you don't try
Deoncia Grayson_1
Honored Contributor

Re: check me for a process and restarting if the process is not found

Hey Everyone,

Thanks for your help. I got it now. :)

De
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon
Deoncia Grayson_1
Honored Contributor

Re: check me for a process and restarting if the process is not found

Thanks for all your help, the scripts helped a lot!
If no one ever took risks, Michelangelo would have painted the Sistine floor. -Neil Simon