Search code examples
pidps

Why does grepping PID keep changing?


I want to write a bashscript to kill a process by ID, but I can't even seem to get the PID working since it keeps changing. Why is this?

Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof              515   0.0  4.3  4822060 723232   ??  U    10:28PM   4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof             4489   0.0  0.0  2436888    812 s002  S+   12:36AM   0:00.00 grep firefox
Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof              515   0.0  4.3  4822060 723232   ??  U    10:28PM   4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof             4491   0.0  0.0  2436888    812 s002  S+   12:36AM   0:00.00 grep firefox
Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof              515   0.0  4.3  4822060 723232   ??  U    10:28PM   4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof             4493   0.0  0.0  2436888    812 s002  S+   12:36AM   0:00.00 grep firefox
Jacks-MBP:~ Knof$ ps aux | grep "firefox"
Knof              515   0.0  4.3  4822060 723232   ??  U    10:28PM   4:57.15 /Applications/Firefox.app/Contents/MacOS/firefox
Knof             4495   0.0  0.0  2435864    800 s002  S+   12:36AM   0:00.00 grep firefox

Solution

  • Every time you fire

    ps aux | grep "firefox"
    

    you are restarting a grep process. It's doesn't indicate the PID of the running Firefox located at /Applications/Firefox.app/Contents/MacOS/firefox

    in your case.

    In your case, 515 is the process to kill.