July 2010
M T W T F S S
« Jun    
 1234
567891011
12131415161718
19202122232425
262728293031  
86

Categories

Archives

[one-liner]: Filtering ps from ps (redux)

As it is with UNIX & Linux there is always another way. In my previous article [one-liner]: Filtering ps from ps, one reader, Christoph, mentioned an alternative method to the one I outlined. In this case, I would consider his to be a better way, so I thought I would take a second to demonstrate this alternative method. The alternative? Use the command pgrep.

The Original Approach

My original post offered the following one-liner:

1
2
3
4
5
6
7
8
9
10
% ps -eaf | grep "[h]ttpd"
root      2683     1  0  2008 ?        00:20:31 /usr/sbin/httpd
apache   17146  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd
apache   17147  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd
apache   17149  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd
apache   17150  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd
apache   17151  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd
apache   17152  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd
apache   17153  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd
apache   17154  2683  0 Aug30 ?        00:00:02 /usr/sbin/httpd

This one-liner provided a list of all the httpd processes running, while filtering out the actual string from the grep httpd command.

The Alternative Approach

By using the command pgrep, the same effect can be achieved and a lot more. For starters, you can get a list of all the httpd PIDs:

1
2
3
4
5
6
7
8
9
# list of httpd PIDs
 
% pgrep httpd
1608
7645
9739
10051
27712
27859

This could be useful in a shell script, if needed, to check for any running httpd processes. For example:

1
2
3
4
# test for httpd processes
 
% [ -z "`pgrep httpd`" ] || echo "running"
running

Here are some other examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# list of PIDs with corresponding command name
 
% pgrep -l httpd
1608 httpd
7645 httpd
9739 httpd
10051 httpd
27712 httpd
27859 httpd
 
# list of PIDs with corresponding command name owned by user root
 
% pgrep -l -u root httpd
1608 httpd
 
# list of PIDs, separated with a comma delimiter
 
% pgrep -d, httpd
1608,7645,9739,14119,14162,27859
 
# detailed list of httpd PIDs via ps
# NOTE: $(...) runs the command above, returning the list of PIDs to ps
 
% ps -fp $(pgrep -d, httpd)
UID        PID  PPID  C STIME TTY          TIME CMD
root      1608     1  0 Aug03 ?        00:00:05 /usr/sbin/httpd
apache    7645  1608  0 Sep04 ?        00:00:47 /usr/sbin/httpd
apache    9739  1608  0 Sep04 ?        00:01:50 /usr/sbin/httpd
apache   14119  1608  0 Sep04 ?        00:00:13 /usr/sbin/httpd
apache   14162  1608  0 Sep04 ?        00:00:13 /usr/sbin/httpd
apache   27859  1608  0 Sep04 ?        00:07:19 /usr/sbin/httpd

Thanks again to Christoph for pointing out this alternative.

NOTE: For further details regarding my one-liner blog posts, check out my one-liner style guide primer.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>