Operating System - HP-UX
1833589 Members
5267 Online
110061 Solutions
New Discussion

Re: test for running proces.

 
SOLVED
Go to solution
someone_4
Honored Contributor

test for running proces.

Hey everyone
Hope everyone had a good weeked!Well here is my question. How would I do
ps -ef|grep dba
and test if dba is running do this else do this. I have been playing with it .. and I just cant figure it out.
Thanks

Richard
7 REPLIES 7
Steven Sim Kok Leong
Honored Contributor
Solution

Re: test for running proces.

Hi,

Script skeleton:
===============
#!/sbin/sh
if ps -ef|grep dba|grep -v grep >/dev/null 2>/dev/null
then
# include your commands here
else
# set alert etc.
fi

Put this script in a cron job.

Example:

#!/sbin/sh

if ps -fu oracle|grep ora_|grep -v grep >/dev/null 2>/dev/null
then
echo Oracle background processes are running
else
echo Oracle background processes are not running
fi

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com
James R. Ferguson
Acclaimed Contributor

Re: test for running proces.

Hi Richard:

At the risk of sounding funny, if you grep for a process by name and you get something returned, then its running. BUT, using your syntax, you should amend it to be:

# ps -ef|grep dba |grep -v "grep dba"

In this fashion you will not be returned the item for which you are grepping.

Remember too, that you would be returned results for any line containing "dba", not just for processes named this!

You would also be returned lines that matched "notdba" for instance which isn't necessarily desired.

You could improve this by doing:

# ps -ef|grep dba$ |grep -v "grep dba$"

...which anchors the search pattern to the end of the line.

You could also express your 'ps' in UNIX95 and look for the basename of the process you wish to match.

Regards!

...JRF...
someone_4
Honored Contributor

Re: test for running proces.

So simple yet so hard..
I was right there but I was going about it a totaly differnt way. A longer and more complicated way. UGG..


Richard
someone_4
Honored Contributor

Re: test for running proces.

That good ole UNIX95 :)
Thanks for the extra info.

Richard

Re: test for running proces.

And if your in to saving cpu cycles, you can ensure you never match on the grep process itself by changing

ps -ef | grep dba | grep -v grep

to

ps -ef | grep [d]ba

If you want a rock solid check on a process, and you know the full name of the process and arguments you can try:
e.g. an oracle pmon for sid PROD

UNIX95= ps -eo args | grep "^ora_pmon_PROD$"

HTH

Duncan

I am an HPE Employee
Accept or Kudo
Bill Hassell
Honored Contributor

Re: test for running proces.

Actually, there is a MUCH more reliable way to find a process by name: don't use grep at all! It turns out that ps has it's own builtin process locater and it works a lot better.

ps -ef|grep dba will find all processes owned by dbarker and processes called fordba3 an bambamdba as well as dba. What you want to search for is the basename of the process as an exact match:

UNIX95= ps -fC dba

And you'll see just the process(es) you are looking for--grep is not necessary (or desirable). The return code is non-zero when there is no match. If there are multiple copies of the same process running, you'll have to count them as in:

COUNT=$( UNIX95= ps -fC dba | wc -l )
COUNT=$((COUNT - 1)) # (don't count header)
if [ $COUNT -gt 1 ]
then
if [ $COUNT -gt 2 ]
then
echo "$COUNT copies are running!"
else
echo "1 copy is running"
fi
else
echo "No copies are running!"
fi


Bill Hassell, sysadmin

Re: test for running proces.

Trust Bill to know a better way of doing something!

I've been doing some playing around with this method, and one thing to bear in mind is that processes like oracle have a habit of changing their argv command lines after they have been started, and although you can see this, the -C option only matches on the original process basename (which is 'oracle' for all oracle processes whether they are server or shadow), so

ps -fC ora_pmon_PROD

returns nothing, but

ps -fC oracle

returns the pmon,smon etc plus the shadow processes.

HTH

Duncan

I am an HPE Employee
Accept or Kudo