2014-01-31 89 views
8

我正在嘗試編寫一個bash腳本來查找正在運行的進程的PID,然後發出kill命令。我有它的部分工作,但我面臨的問題是可能有多個進程運行。我想對每個發現的PID發出kill命令。查找正在運行的進程的PID並作爲數組存儲

我想我需要把每個PID放到一個數組中,但是在如何做到這一點上卻一直處於虧損狀態。

我有什麼至今:

pid=$(ps -fe | grep '[p]rocess' | awk '{print $2}') 
if [[ -n $pid ]]; then 
    echo $pid 
    #kill $pid 
else 
echo "Does not exist" 
fi 

這是什麼需要做的是回到每一條線全部PID,但我無法弄清楚如何將數組分割這一點。

+2

檢查'killall'或'pkill'命令來保存自己大量的工作。 –

回答

19

這裏有一個小一個襯墊,可以幫助

for pid in `ps -ef | grep your_search_term | awk '{print $2}'` ; do kill $pid ; done 

只需更換your_search_term您要終止的進程的名稱。

你也可以將它變成一個腳本,並交換your_search_term$ 1

編輯:我想我應該解釋一下這是如何工作的。

back ticks``從裏面收集表達式的輸出。在這種情況下,它將返回進程名稱的pid列表。

使用for循環我們可以遍歷每個pid並殺死進程。

EDIT2:代替殺-9殺,如果你想有一個新的像每個PID名單,然後更換

+2

除非你知道流程很可能無法順利關閉,否則不要「殺死-9」。 – BroSlow

+0

是的,你是正確的,哈比人真的從我自己的力量:)我不認爲這是值得downvote,雖然... – SparkyRobinson

+0

另外,'grep A | awk'{B}''幾乎總是寫得更好'awk'/ A/{B}''。如果你想傳遞一個正則表達式作爲參數,'awk -v param = A'$ 0〜param {B}' – tripleee

0

你的腳本似乎罰款,:

echo $pid 
#kill $pid 

echo "$pid" 
#kill "$pid" 
+0

這是個糟糕的建議。如果沒有引號,shell會將變量的值拆分爲空白的標記,因此當變量包含多個PID時,'kill'將它們作爲位置參數接收。 *使用*引號,變量作爲單個參數傳遞給'kill',使用換行符和全部,這不是'kill'的有效參數。因此,只有當變量包含單個PID並且不提供任何值(或者實際上,只有負值)時,引號纔會發生意外。 – tripleee

2

如果您要立即迭代結果並執行操作,則不需要使用數組:

for pid in $(ps -fe | grep '[p]rocess' | grep -v grep | awk '{print $2}'); do 
    kill "$pid" 
done 

請注意,我們必須從要殺死的進程列表中排除grep的pid。或者,我們可以只使用pgrep(1)

for pid in $(pgrep '[p]rocess'); do 
    kill "$pid" 
done 

如果你確實需要的PID存儲在數組中,pgrep是你會怎麼做:

pids=($(pgrep '[p]rocess')) 

回殺的過程。我們仍然可以做得更好。如果我們只是使用pgrep來獲取殺死它們的進程列表,爲什麼不直接去找pgrep的姐妹程序:pkill(1)

pkill '[p]rocess' 

事實證明,根本不需要bash腳本。

2

不知道爲什麼你會想要殺死一個進程,除非你不知道命令名。的大多數現代版本具有標誌

-C cmdlist 
      Select by command name. This selects the processes whose executable name is given in cmdlist. 

-o format 
      User-defined format. format is a single argument in the form of 
      a blank-separated or comma-separated list, which offers a way to 
      specify individual output columns. The recognized keywords are 
      described in the STANDARD FORMAT SPECIFIERS section below. 
      Headers may be renamed (ps -o pid,ruser=RealUser -o 
      comm=Command) as desired. If all column headers are empty (ps 
      -o pid= -o comm=) then the header line will not be output. 
      Column width will increase as needed for wide headers; this may 
      be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE- 
      WCHAN-COLUMN -o comm). Explicit width control (ps opid, 
      wchan:42,cmd) is offered too. The behavior of ps -o pid=X, 
      comm=Y varies with personality; output may be one column named 
      "X,comm=Y" or two columns named "X" and "Y". Use multiple -o 
      options when in doubt. Use the PS_FORMAT environment variable 
      to specify a default as desired; DefSysV and DefBSD are macros 
      that may be used to choose the default UNIX or BSD columns. 

所以,你可以做

ps -o pid= -C commandName 

將返回這裏命名命令名的所有進程的PID,是更清潔和更快速。或殺死一環

while read -r pid; do 
    kill "$pid" 
done < <(ps -o pid= -C commandName) 

不過說真的,你應該永遠只能夠做

> pkill commandName 
相關問題