Monday, February 26, 2007

Handy *nix Commands, Part 1: Finding Processes

On countless occasions, I have needed to find the PID of a process, in order to watch it or, more often, to kill it. For quite some time, I would simply run "ps ax | grep" followed by part of the process' name that I wanted to find. (Side note: it helps to also add " | grep -v grep" to this to eliminate the grep itself from the results.)

This works, but it is slow to use. I had to read through the results of ps to get the PID, when that is often all I wanted. One could use awk to grab just the PID column, but there are faster solutions:
  • pgrep (man) - Looks up processes based on options and displays matching PID. Helpful options are -f, finds text match of process name, -o finds oldest matching process.
  • pidof (man) - finds PID of a matching program name.
In both cases, you just pass the command the name (or part of it) of the process you want to find, and you get all matches. You can use pkill, or pipe to kill, to initiate the demise of the process once you find it.


Post a Comment

Subscribe to Post Comments [Atom]

<< Home