Alexander Kleinhans
Alexander Kleinhans

Reputation: 6248

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

Upvotes: 2

Views: 4517

Answers (2)

xCodeZone
xCodeZone

Reputation: 194

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.

Upvotes: 4

Torbjörn
Torbjörn

Reputation: 5800

Each time a process is started, it is assigned a new (incrementing) PID; even if the executable and all arguments are the same.

You'll notice the PID of "firefox" is the same for all of your four calls, indicating that is hasn't been restarted meanwhile.
The PID for "grep" changes as it has been started (and terminated) for each call.

Upvotes: 2

Related Questions