1837981 Members
1873 Online
110124 Solutions
New Discussion

Re: use of grep

 
ust3
Regular Advisor

use of grep

I use grep to find process in the system , it works but I found that the output includes the self process of grep , like the example below , the last line "grep sendmail" is the grep process not the sendmail process , can advise how to exclude this output ? thx



ps -ef |grep sendmail

root 3875 1 0 2007 ? 00:03:40 sendmail: accepting connections
smmsp 3875 1 0 2007 ? 00:00:01 sendmail: Queue runner@01:00:00
for /var/spool/clientmqueue
root 5875 5874 0 15:00 ? 00:00:00 /usr/sbin/sendmail -FCronDaemon
-i -odi -oem -oi -t
root 28078 23007 0 17:02 pts/17 00:00:00 grep sendmail


16 REPLIES 16
Steven Schweda
Honored Contributor

Re: use of grep

It's not fool-proof, but:

ps -ef | grep sendmail | grep -v grep
Oviwan
Honored Contributor

Re: use of grep

Hey

use the -v parameter:

ps -ef |grep sendmail | grep -v grep

check man grep for -v

Regards
MarkSyder
Honored Contributor

Re: use of grep

ps -ef|grep sendmail|grep -v grep

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
ust3
Regular Advisor

Re: use of grep

thx replies.

ust3
Regular Advisor

Re: use of grep

thx replies,

it works , but I found that same problem if my command is as below , while the directory have sendmail process id in it , can advise how to fix it ? thx.

ll directory |grep sendmail |grep -v grep

Asif Sharif
Honored Contributor

Re: use of grep

#ps -ef|grep sendmail|grep -v grep
root 1311 1 0 09:42:42 ? 0:01 sendmail: accepting connections

#man ps

Regards,
Asif Sharif
Regards,
Asif Sharif
Oviwan
Honored Contributor

Re: use of grep

post the out of:
ll directory |grep sendmail |grep -v grep
Matti_Kurkela
Honored Contributor

Re: use of grep

Here's a little trick:

ps -ef | grep [s]endmail

The regexp "[s]endmail" matches the word "sendmail" only, but when it's expressed in this way, the line of the grep process won't match the regexp and so won't get included.

MK
MK
Hein van den Heuvel
Honored Contributor

Re: use of grep

Just grep for something using a 'regular expression'. The extra characters will make the search itself not match the target.

For example:

ps -ef | grep sendmai[l]

hth,
Hein.
Aneesh Mohan
Honored Contributor

Re: use of grep

Hi,

Use the below command to find correct output from the process table.

example : top process

#UNIX95=1 ps -f -C top
UID PID PPID C STIME TTY TIME CMD
root 335 323 0 06:53:11 pts/1 00:00:00 top

If you want to grep only then ,
# ps -aef |grep top|grep -v grep
root 335 323 0 06:53:11 pts/1 0:00 top
#


Thanks,
Aneesh



ps -aef |grep top|grep -v
ust3
Regular Advisor

Re: use of grep

thx replies,

what my problem is when I use the command ll , the ll itself is a process , it will write the process id to the directory (the directory contains all process id ), what I want is use "ll directory |grep -i pid |grep -v grep" , and it do not show the process which generated from ll , can advise how to do it ? thx
MarkSyder
Honored Contributor

Re: use of grep

ll and ps are completely different commands. ll on a directory:

1. will not show pids

2. will not include the ll command.

Can you please clarify what you are trying to do?

Mark
The triumph of evil requires only that good men do nothing
Dennis Handly
Acclaimed Contributor

Re: use of grep

>what my problem is when I use the command ll, the ll itself is a process, it will write the process id to the directory

"it" who? How does ll(1) write the PID to the directory?

>I want is use "ll directory | grep -i pid | grep -v grep", and it do not show the process which generated from ll, can advise how to do it?

How did we get from using the -C option for ps(1) to ll(1)?

You had a question about:
ll directory |grep sendmail |grep -v grep
>the directory contains all process id)

Are you saying you created a filename with the PID and the process name and arguments?

We need to see the results of:
ll directory | grep sendmail
Matti_Kurkela
Honored Contributor

Re: use of grep

ust3, I think you're talking about some unix-like OS that has a /proc filesystem or something similar. HP-UX does not have that feature (at least not normally!).

If an OS has a /proc filesystem, it usually lists processes by process ID numbers, not by process names. I'm not aware of any OS that would create a directory listing of process names.

As this question is on a HP-UX administration forum, we obviously tend to assume that you're asking about HP-UX unless you explicitly say otherwise.

If you know your question is specific to Linux or Tru64 (Digital Unix), please use the forum sections that are dedicated to those OSs, to avoid this kind of confusion.

MK
MK
Arturo Galbiati
Esteemed Contributor

Re: use of grep

Hi ust3,
better way to fix your problem is to use the UNIX95 as already suggested.
A possible solution is that you write teh pid of teh process into a file and after use it to look for your process using ps -p.
Just .02$
HTH,
Art
Bill Hassell
Honored Contributor

Re: use of grep

Never, ever use grep and ps together until you understand how unreliable this combination will be. ps is 1000x better at finding an exact PID than grep. Use the command:

ps -f -p 123

And you get exactly that process ID.

If you want to find all processes owned by a particular user, use ps -u like this:

ps -f -u billh

You never want to search for a process by name using grep -- use the -C option in ps. For the -C option, a temporary variable must be used to enable the -C option, like this:

UNIX95=1 ps -f -C sh

Compare the output of the above command with this:

ps -e -f | grep sh

This illustrates the very unstable nature of using grep with ps. grep does not know what you are looking for -- it matches anything on the line.

Once you read the man page for ps, you'll never use ps and grep together again.


Bill Hassell, sysadmin