2016-07-27 32 views
1

我想编写一个bash脚本来通过ID来杀死一个进程,但我似乎无法让PID工作,因为它不断变化。为什么是这样?为什么grepping PID不断变化?

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 

回答

2

每次开火

ps aux | grep "firefox" 

你重新启动grep的过程。这并不表示运行Firefox的PID位于 /Applications/Firefox.app/Contents/MacOS/firefox

您的情况。

在你的情况下,515是杀死的过程。

+0

你打秒我...... ;-) –

2

每次启动进程时,都会分配一个新的(递增的)PID;即使可执行文件和所有参数相同。

您会注意到,“firefox”的PID对于您所有的四个调用都是相同的,这表明它同时没有重新启动。
“grep”的PID会因每次调用已启动(并终止)而发生变化。