Tuesday, November 20, 2007
2:18 AM

How to Look Up a Process

You can look up a process in many ways. Often, you have the name of a daemon, command, or a program, and you want to know its process ID. For example, you may want to find the process ID of firefox.

  • pgrep

    $ pgrep firefox
    4559


    You do not need to specify the complete name.

    $ pgrep fox
    4559


    Because of the partial matching, it is prudent to have pgrep report both the process ID and the matching name:

    $ pgrep -l fox
    4559 firefox-bin


  • ps
    $ ps aux |grep fox 
    peter 4559 4.0 8.7 211680 84668 ? Sl 08:58 6:20 /usr/lib/iceweasel/firefox-bin -a firefox
    root 10611 0.0 0.0 2848 696 pts/0 R+ 11:36 0:00 grep fox


    Alternatively,
    $ ps -ef |grep fox
    peter 4559 1 4 08:58 ? 00:06:26 /usr/lib/iceweasel/firefox-bin -a firefox
    root 10645 8518 0 11:38 pts/0 00:00:00 grep fox


    The second output line for each of the 2 commands refers to the grep itself, and should be ignored.


The pgrep and the ps approaches can be adjusted to look up a process using other criteria besides the name.

For example, to look up all processes being executed by an user ID (0 = root):


  • $ pgrep -lu 0
    pgrep -lu 0
    1 init
    2 migration/0
    3 ksoftirqd/0
    4 events/0
    5 khelper
    6 kthread
    9 kblockd/0
    ....


  • $ ps -ef |grep root 
    root 1 0 0 08:56 ? 00:00:01 init [2]
    root 2 1 0 08:56 ? 00:00:00 [migration/0]
    root 3 1 0 08:56 ? 00:00:00 [ksoftirqd/0]
    root 4 1 0 08:56 ? 00:00:00 [events/0]
    root 5 1 0 08:56 ? 00:00:00 [khelper]
    root 6 1 0 08:56 ? 00:00:00 [kthread]
    root 9 6 0 08:56 ? 00:00:00 [kblockd/0]
    ...

    The grep command will match root along anywhere on the output line. This is sufficient for most casual command line lookup of processes. Of course, you can write to your heart's content a pattern-matching awk/perl script to only output lines that have root in the user name field.


For details, please read the pgrep and ps man pages.

StumbleUpon Toolbar

0 comments:

Post a Comment