Operating System - HP-UX
1753774 Members
7136 Online
108799 Solutions
New Discussion юеВ

Re: To start or not to start.......

 
SOLVED
Go to solution
Deborah Grierson
Frequent Advisor

To start or not to start.......

We have a critical process which stops for no apparent reason and at any time during the day. I'm working on why that is, but meanwhile, I need to run a cron script at regular intervals to check whether the process is still running, and if it isn't, to restart it.

Can I get some return value from a 'ps -ef' command which indicates whether the particular process is running?

John
I'll need all the help I can get
8 REPLIES 8
Paula J Frazer-Campbell
Honored Contributor

Re: To start or not to start.......

Hi

ps -ef | grep -v grep | grep | wc -l

Will return 1 if running and 0 if not.

If 0 then restart.


Paula
If you can spell SysAdmin then you is one - anon
Paula J Frazer-Campbell
Honored Contributor

Re: To start or not to start.......

John

Further to my last apart from restarting it you neen to find out why it is stopping.

If you instal "tusc" it will allow you to attach to the pid and monitor what it is doing.

Tusc can be downloaded here:-

http://hpux.connect.org.uk/

HTH

Paula
If you can spell SysAdmin then you is one - anon
Deborah Grierson
Frequent Advisor

Re: To start or not to start.......

Paula

Thanks for your prompt reply. I know I'm really dumb, but how do I test for a returned 1 or 0 ?

John
I'll need all the help I can get
Ceesjan van Hattum
Esteemed Contributor

Re: To start or not to start.......

You should not write a script (like cron) or script with while : do ...; sleep 5 done
because this takes processtime and will not react immediately for your critical proces.
Write a fork-ing script. The child-process will start your critical proces, in case the process dies, the child dies and the father proces will immediataly start a new child process. This will take about 2 milliseconds. The fork-ing process should run like a daemon and therefor sleeps and will not take as much resources...
I have such scripts, but is propriaty software which i can not distribute..

Regards,
Ceesjan
Paula J Frazer-Campbell
Honored Contributor
Solution

Re: To start or not to start.......

Hi John

Not dumb - we all have to learn.

Hi Again

In a script :-



----------------------------cut here-------------------------------------

#!/bin/sh
#######################################
# Restart if not running
#######################################
# Is it running?
rs=`ps -ef | grep -v grep | grep ytest | wc -l`
if [[ $rs != 1 ]]
then
# Restart it
/path/to/program
fi
# Wait 10 mins and check again
sleep 600
# restart this program
exec /sysadmin/test
---------------------------cut here-----------------------------------------


The last line calls the test program - so basically it checks to see if program is running if OK does nothing , if not running it restarts it.

Sleeps for 600 seconds, then restarts itself.

start it with a nohum command - ie


# nohup /scrip/name &

HTH

Paula
If you can spell SysAdmin then you is one - anon
Paula J Frazer-Campbell
Honored Contributor

Re: To start or not to start.......

John

the ytest in the ps -ef line must be replaced with your process name.

Further - the script checks to see if the ps -ef test is not equal to 1 -

Be aware the 2 is not equal to 1 and that multiple instances are not trapped.

Paula

If you can spell SysAdmin then you is one - anon
Ralph Grothe
Honored Contributor

Re: To start or not to start.......

Hi John,

a standard idiom to test wether a process is alive, is to send its PID a "NULL" signal

e.g.

assume your proc has PID 1111

while kill -0 1111 2>/dev/null; do
# idle here
sleep 10
done
# then restart your proc

You asked how to check a proc's return code,
it's stored in the special variable $?

e.g.

if [ $? -ne 0 ]; then
echo "ouch, something went wrong" >&2
fi

You may also combine this in the condition:

if UNIX95= ps -e -o comm= | grep -q your_proc_name; then
echo is alive
fi


Madness, thy name is system administration
Deborah Grierson
Frequent Advisor

Re: To start or not to start.......

Paula, many thanks for your help. I'll implement solution and hope that the damn process falls over! (lol)

Ralph, thanks for your answer(s). I'll need time to study them before I try them out.

Ceesjan, thanks for the advice, but system resources are not a problem.

John
I'll need all the help I can get