1834060 Members
2540 Online
110063 Solutions
New Discussion

Re: grep artifacts

 
SOLVED
Go to solution
Gilbert Standen_1
Frequent Advisor

grep artifacts

Hi, if do "ps -ef | grep pmon" sometimes I just get
oracle 962572 1 0 Jan 11 - 2:13 ora_pmon_ATCQ2

but sometimes I get pmon, and grep also catches "itself" i.e. same command as above returns:
oracle 962572 1 0 Jan 11 - 2:13 ora_pmon_ATCQ2
oracle 1495276 2539608 0 08:55:20 pts/1 0:00 grep pmon

However, if I type "ps -ef | grep pmon | grep ATCQ2" it always returns just the proc of interest, and grep never detects itself as above.

My question is, why should this be the case ? I use ps -ef often for program control steps such as running parallel commands in their own shell, and the feature of grep was something found by trial and error, but I wondered if there is a reason for it.

If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
9 REPLIES 9
Tom Danzig
Honored Contributor

Re: grep artifacts

Depending on the CPU click tic, you can and usually do get the grep itself also.

It would be safer to do "ps -e | grep pmon" instead of "ps -ef | grep pmon". This will eliminate the grep.

You could also do "ps -ef | grep pmon | grep -v grep", however, this adds yet another unnecessary system call.
Patrick Wallek
Honored Contributor

Re: grep artifacts

Sometimes your just lucky enough to catch the grep process, sometime not.

If you are doing this within a script the way to solve it is to add an extra 'grep -v grep' to your command. That way the grep process itself will be ignored.

So your command would be:

ps -ef | grep process | grep -v grep
Henk Geurts
Esteemed Contributor

Re: grep artifacts

hi Gilbert

the correct way is to use the
ps -ef|grep XXX |grep -v grep
regards
Henk
MarkSyder
Honored Contributor

Re: grep artifacts

The first grep in your command takes its input from ps -ef and will therefore output two lines.

The second grep takes it input from the first one: i.e. the two lines described above. As only one of these will have ATCQ2 in it, this is the only line you will see.

You could achieve the same result by:

ps -ef|grep pmon|grep -v grep

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Bill Hassell
Honored Contributor
Solution

Re: grep artifacts

Actually, grep should be avoided when filtering the ps command. ps has many, many options to give you an EXACT match of what you are looking for and never 'catches' itself so you never have to grep -v grep.

To find an exact command name:
UNIX95= ps -fC mycommand

For example, compare the following:
ps -ef | grep sh
UNIX95= ps -fC sh

You'll notice that the second form ALWAYS matches the process name (sh) while the first method matches sh, ksh, bash, unhashdaemon, sshd and so on. The -C option is disabled by default but turned on whenever UNIX95 is defined. You can define it with just UNIX95= or set it to any value such as UNIX95=1. The ps program doesn't care. DO NOT export UNIX95 on a separate line!! This is a pervasive variable that when it is set, will change the behavior of several libraries (and thus applications) as well as certain programs. In the case of ps, just include it on the same line (as shown above) and it disappears once the command is finished.

There are dozens of options to select proceses, and two additional UNIX95 values: -H and -o. Try this:

UNIX95= ps -eH

which shows all the parent/child relationships. Add -f to verify the PID/PPID relationships:

UNIX95= ps -efH

And because -f has extra items, you can use -o to customize your own ps output:

UNIX95= ps -eH -o pid,ppid,ruser,args

You can even customize the headings:


UNIX95= ps -eH -o pid=ProcNumber -o ppid="MY parent" -o ruser="Real User" -o args="Command Line"

The -o option is very sueful for scripting.


Bill Hassell, sysadmin
MarkSyder
Honored Contributor

Re: grep artifacts

As always, Bill has trumped us all!

Gilbert, give that man 11 points!

Mark
The triumph of evil requires only that good men do nothing
Gilbert Standen_1
Frequent Advisor

Re: grep artifacts

Hi,
I tried this on my UNIX box and here is what I got:

# UNIX95= ps -fC sh
ps: Not a recognized flag: C
Usage: ps [-ANaedfklm] [-n namelist] [-F Format] [-o specifier[=header],...]
[-p proclist][-G|-g grouplist] [-t termlist] [-U|-u userlist] [-c classlist]
Usage: ps [aceglnsuvwxU] [t tty] [processnumber]

Similar problems were encountered with the other commands mentioned. Do I have something that is not configured correctly ?

Gil


If I could take one thing with me into the next world it would be my valid login to HP ITRC Forums
Patrick Wallek
Honored Contributor

Re: grep artifacts

What version of HP-UX are you running? The UNIX95 stuff should work.
Bill Hassell
Honored Contributor

Re: grep artifacts

I hope you're not using csh...
The error message you're seeing is due to ps not seeing the UNIX95 flag, so the shell you are using is not POSIX standard (ie, not ksh, bash, sh for HP-UX, etc). What do these commands show?

ps
echo $SHELL

And I am assuming you're running HP-UX later than 9.0..(uname -r)


Bill Hassell, sysadmin