2013-03-12 23 views
2

我人在運行此命令:殺死自動處理 - Linux的

pgrep -l someprocess 

我得到一些輸出XXXX someprocess

然後我殺的手出現每一個過程,我想編寫一個腳本來自動執行,不過這並未(T意義 kill -9 $(pgrep -l someprocess | grep "^[0-9]{4}")

有人可以幫助?

回答

5

您可以使用或者pkillkillall即可完成此操作。

我找到了this簡短明瞭的總結,說明了kill ing過程的不同方式。

pkill就像這樣簡單:pkill someprocess

@ewm在他的回答中已經包含了關於killall的詳細說明,所以我在此不再重複。

+0

謝謝,我不知道! – user1611830 2013-03-12 18:28:36

+0

只是好奇,如果我不得不用手寫,我會怎麼做? – user1611830 2013-03-12 18:29:23

+0

查看我的更新回答 – shx2 2013-03-12 19:10:52

3

你可能想看看 'killall' 命令:

KILLALL(1)用戶命令KILLALL(1)

名稱 killall - 的名字來殺死

概要 killall [流程-Z, - 上下文模式] [-e, - exact] [-g, - process-group] [-i, - interactive] [-q, - quiet] [-r, - regexp ] [-s, - 信號信號] [-u, - 用戶用戶] [-v, - 詳細] [-w, - wait] [-I, - ignore-case] [-V, --version] [ - ]名稱... killall -l killall -V, - 版本

說明 killall向運行任何指定命令的所有進程發送信號。如果沒有指定信號名稱 ,則發送SIGTERM。

Signals can be specified either by name (e.g. -HUP or -SIGHUP) or by number (e.g. -1) or by option 
    -s. 

    If the command name is not regular expression (option -r) and contains a slash (/), processes execut- 
    ing that particular file will be selected for killing, independent of their name. 

    killall returns a zero return code if at least one process has been killed for each listed command, 
    or no commands were listed and at least one process matched the -u and -Z search criteria. killall 
    returns non-zero otherwise. 

    A killall process never kills itself (but may kill other killall processes). 
+0

+1'killall'更加無處不在。許多平臺沒有'pkill'。 'sed' /'grep','ps'和'kill'也是選項。 'kill -9 $(ps -ef | grep [Tt] arget | sed -e's/[A-Za-z] * * \([0-9] [0-9] * \)。*/\ 1 /')'會在捏,但更脆。 – 2013-03-24 23:44:35