Operating System - HP-UX
1833784 Members
4207 Online
110063 Solutions
New Discussion

grep command does not show up

 
SOLVED
Go to solution
Jean-Luc Oudart
Honored Contributor

grep command does not show up

Folks,

To find out if a specific application is down (or finished) we count the number of processes (with ps -ef | grep xxx).
Sometimes the "grep" does not show up.
cf. the following example :
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
N4000:pmurphya:/lprd1/lprd/work $ ps -fu pmurphya | grep lprdet
pmurphya 26576 16765 1 11:58:31 pts/tVb 0:00 grep lprdet
pmurphya 25268 1 70 11:57:10 pts/tcc 0:00 lprdet.exe etbase 0
pmurphya 25371 1 63 11:57:12 pts/tcc 0:00 lprdet.exe etbase 1
pmurphya 25471 1 64 11:57:14 pts/tcc 0:00 lprdet.exe etbase 2
pmurphya 24925 24851 57 11:56:54 pts/tcc 0:00 lprdetcn.exe etctrl
N4000:pmurphya:/lprd1/lprd/work $ ps -fu pmurphya | grep lprdet
pmurphya 25268 1 68 11:57:10 pts/tcc 0:00 lprdet.exe etbase 0
pmurphya 25371 1 63 11:57:12 pts/tcc 0:00 lprdet.exe etbase 1
pmurphya 25471 1 64 11:57:14 pts/tcc 0:00 lprdet.exe etbase 2
pmurphya 24925 24851 59 11:56:54 pts/tcc 0:00 lprdetcn.exe etctrl
N4000:pmurphya:/lprd1/lprd/work $
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I suggested to the application to reduce count to zero ancd change the command with a pipe to rep -v grep.
But still would like to know why this is happening ?

thanks for your contribution,
Jean-Luc
PS : this is running on N4000 / HPUX11.0
fiat lux
10 REPLIES 10
Steven Mertens
Trusted Contributor
Solution

Re: grep command does not show up

hi,

It depends probably about the speed the
command is executed. A better way to do this
is : ps -fu pmurphya |grep [l]prdet

The grep command will never show up then.

regards,

Steven
Bart Beeren
Advisor

Re: grep command does not show up

Hi,

to my opinion it is some timing "problem" why sometimes the grep itself shows up (It happened to me before) . When you always want to be sure about the correct number of processes do the grep -v like you said yourself:

ps -ef|grep xxx|grep -v grep

BB
Life isn´t as simple as it seems
harry d brown jr
Honored Contributor

Re: grep command does not show up

I've used the

command | grep somestring | grep -v grep

for a long time, but with Steven's example, I probably won't be using the

... | grep -v grep

any longer! Good work Steven!

live free or die
harry
Live Free or Die
Bart Beeren
Advisor

Re: grep command does not show up

at first I misinterpreted Steven's solution. After the response of Harry I realised what an easy solution it is and that it will work in all cases fine.
BB
Life isn´t as simple as it seems
Pete Randall
Outstanding Contributor

Re: grep command does not show up

I like Steven's technique but I'm not sure I could ever re-train my fingers - I've been using the following "psg" script for too long:
# cat /usr/local/bin/psg
##########################################################################
# psg - Process Search
# Substitute for "ps -ef|grep" command string.
##########################################################################
case $# in
0) echo "Error: Argument Expected";
exit;
;;
1) ps -ef | grep $1 | grep -v "grep"
;;
*) echo "Error: Only 1 Argument Allowed";
exit;
;;
esac

Pete

Pete
Jean-Luc Oudart
Honored Contributor

Re: grep command does not show up

Thanks to all , especially Steven. Now I can even spare another process in my command :
ps -ef | grep -c lprde[t]
and I get my number of process directly !

JL
fiat lux
Bill Hassell
Honored Contributor

Re: grep command does not show up

This has been posted to the forums before, but NEVER use grep with ps when you are looking for a process name! Consider what happens when you are looking for the sh processes--you get "ksh", "csh", "bash", even "washer". Instead, use ps's builtin search capability (which is enabled with XPG4 behavior):

UNIX95= ps -fu murphya -C lprdet

ps is smart enough to look at the process table and find an exact string match for lprdet (and it uses the basename, not the full pathname). In the above example, you can search for all sh processes with:

UNIX95= ps -fC sh

The UNIX95 variable should only be set temporarily (as in the examples, it is set to null) since exporting UNIX95 will cause several commands and libraries to change behavior. Two additional (and very useful) features are activated with UNIX95: -H (for a list of processes by hierarchy) and -o (which allows for a fully customized appearance.

For example:

UNIX95= ps -efH | more

UNIX95= ps -e -o ruser,vsz=Kbytes -o pid,args=Command-Line

In the second example, the real userID is listed, followed by the program's size but with the label changed to Kbytes, then pid and finally the command line, also with a new label.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: grep command does not show up

Hi:

The snapshot of the processes running missed the time when your 'grep' was active. The technique of:

# ps -ef|grep mything|grep -v grep

...is the classic approach.

A better way to find (or count) a particular running process which avoids the need for multiple 'grep's *and* avoids matches for processes you *don't* want is this:

UNIX95= ps -fC tar

Note that the UNIX95 variable is set only for the duration of the command line. 'ps' is used to find *only* processes with a basename (here, by example) of "tar". See the 'ps' man pages for more details.

Regards!

...JRF...
A. Daniel King_1
Super Advisor

Re: grep command does not show up

If I understand correctly, proc-sz (from sar) lists the current and max simultaneous processes:

$sar -v 1

HP-UX fiserv B.11.00 B 9000/800 07/22/02

10:43:55 text-sz ov proc-sz ov inod-sz ov file-sz ov
10:43:56 N/A N/A 459/4020 0 0/5840 0 22431/38890 0

This thread may also prove enlightening:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x5c9703bbece8d5118ff40090279cd0f9,00.html
Command-Line Junkie
A. Daniel King_1
Super Advisor

Re: grep command does not show up

I know that (the sar note) doesn't answer the ultimate question about ps|grep, but this thread got me thinking. Is my understandning about proc-sz correct?

More along those lines, ps|grep seems to become much less dangerous if you use:

UNIX95= /usr/bin/ps -e -ocomm | /usr/bin/grep "^sh$" | /usr/bin/wc -l

using the regex items for begin and end of line.
Command-Line Junkie