We are the Dev Teams of
  • brands
  • ebay_main
  • ebay
  • mobile
<
>
BLOG

Combination of ps and grep - How to Prevent the Grep Process From Showing Up in the Results

by Patrick Hund
in Tutorials

When grepping for a process ID to kill the process, more often than not you get the process ID of the grep command itself:

$ ps -U pahund | egrep '^ *([0-9]+).*navx'
49447   ??  R      5:01.80 /usr/bin/navx -c -r -o /tmp/schedScanFCgWPm -u 0DFB7D29-5CFC-44ED-9C5C-BD0685E539B3 /Volumes/PatricksTimeMachineDisk
51226 s000  S+     0:00.00 egrep --color=auto ^ *([0-9]+).*navx

You can prevent that by piping the grep result to another grep with the -v option, which filters lines that match the grep expression:

$ ps -U pahund | egrep '^ *([0-9]+).*navx' | grep -v grep
49447   ??  R      5:03.65 /usr/bin/navx -c -r -o /tmp/schedScanFCgWPm -u 0DFB7D29-5CFC-44ED-9C5C-BD0685E539B3 /Volumes/PatricksTimeMachineDisk

This is particularly handy if you want to write a script that kills a process.

Grep, PS

?>