1832602 Members
2272 Online
110043 Solutions
New Discussion

Re: ps -elf | grep ???

 
Sylvain CROUET
Respected Contributor

ps -elf | grep ???

Hello.

I would like to automatically know if some processes are running using a generic 'ps -lef | grep ' expression. The problem is that I do not find the correct expression.
For now is "[ |/]". When I am looking for a process named vmd, it will not find a process named cmlvmd, but it can find a process named vmdix, which is not correct.
Has someone ideas to complete my expression?
14 REPLIES 14
Stefan Farrelly
Honored Contributor

Re: ps -elf | grep ???


I think you want;

grep "^vmid$"

which means a process which starts with vmid and ends with vmid.
Im from Palmerston North, New Zealand, but somehow ended up in London...
T G Manikandan
Honored Contributor

Re: ps -elf | grep ???

Just try with a space at the beginning and end of the grep string like

ps -ef|grep " vmd "


Thanks
twang
Honored Contributor

Re: ps -elf | grep ???

To grep ps 's output containing string which starts with 'vmd', try this:
# ps -ef | egrep vmd
TOMAS BERNABEU
Frequent Advisor

Re: ps -elf | grep ???

NAME=$(ps -e|awk '($NF=="name-process"){ print $4 }')

or

PID=$(ps -e|awk '($NF=="name-process"){ print $1 }')


Tom
Sylvain CROUET
Respected Contributor

Re: ps -elf | grep ???

It seems I was not very clear with my question.
First point, I want an unique expression to look after a process, as I want to use it within a script.
Second point, I must use the -x option for ps to differentiate some processes (eg : Xtalk rulemanager process).
Third point, I do not want false positive. When I am looking for a process named vmd, I do not want to find cmlvmd nor vmdix. The $ anchoring will work for this case, but not for the syslogd process (launched with -D option, which appear due to the -x option of the ps command).

I know my question is quite complex, but that is the reason why I need your help. ;-)
Sylvain CROUET
Respected Contributor

Re: ps -elf | grep ???

Actually, the best solution I have found is the following one:
ps -ex | awk '{printf("%s \n", $0)}' | grep "[ |/] "

If someone can find something shorter, without the awk for example, I would be very interested.
Mark Grant
Honored Contributor

Re: ps -elf | grep ???

This can be a problem and all the above solutions are the good appraoches aproaches. Personally, I try and avoid "ps" commands being run every five minutes or so I tend to use the following method were practicable .

Run the application from a script which then goes to sleep. If the application dies, the script will receive a SIGCHLD which you can process to either alert, restart the application or whatever. In other words, rather than checking if the process is running, be informed when it isn't.

Of course, this doesn't work if the application does the traditional fork/exit but it can be useful for monitoring running apps, especially if you use one generic application starting script.
Never preceed any demonstration with anything more predictive than "watch this"
Leif Halvarsson_2
Honored Contributor

Re: ps -elf | grep ???

Hi,
Have you tried the -w option ?
T G Manikandan
Honored Contributor

Re: ps -elf | grep ???

You can also use the inbuilt variable UNIX95 for the processes

UNIX95= ps -e -o 'pid,vsz,comm'

you can retrieve the reqd. colums.


Sylvain CROUET
Respected Contributor

Re: ps -elf | grep ???

Which -w option? I do not find any -w option for ps nor grep.
T G Manikandan
Honored Contributor

Re: ps -elf | grep ???

The -w option of grep comes with the GNu version which can be had from here

http://hpux.cs.utah.edu/hppd/hpux/Gnu/grep-2.5.1/
Leif Halvarsson_2
Honored Contributor

Re: ps -elf | grep ???

Hi,

Frpom the man page of grep:



-w Select only those lines containing matches
that form whole words. The test is that the
matching substring must either be at the
beginning of the line, or preceded by a non-
word constituent character. Similarly, it
must be either at the end of the line or
followed by a non-word constituent character.
Word-constituent characters are letters,
digits, and the underscore.

I run HP-UX 11.11 with the standard grep (no GNU-version).
Sylvain CROUET
Respected Contributor

Re: ps -elf | grep ???

Thanks, Leif and TG.
It seems it is exactly what I need. I will test it as soon as possible.
john korterman
Honored Contributor

Re: ps -elf | grep ???

Hi,

does this work on your system?

# UNIX95= ps -e -opid -ocomm| grep "[[:space:]]vmd$"

Btw, I think the -w option for grep in hpux is only for 11.11 and later.

regards,
John K.
it would be nice if you always got a second chance