2016-07-27 76 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會因每次調用已啓動(並終止)而發生變化。