1) 查看進程的方法:
ps -ef 或者 ps aux
root 15087 0.0 0.0 0 0 ? S 23:31 0:00 [kworker/1:1]
root 15219 0.0 0.0 0 0 ? S 23:36 0:00 [kworker/1:0]
root 15460 0.0 0.0 0 0 ? S 23:41 0:00 [kworker/1:2]
homer 15572 23.0 5.8 791472 119788 ? Sl 23:45 0:03 /usr/lib/firefox/firefox
homer 15590 0.1 0.1 273796 3132 ? Sl 23:45 0:00 /usr/lib/at-spi2-core/at-spi-bus-launcher
homer 15620 0.0 0.0 22360 1268 pts/0 R+ 23:45 0:00 ps aux
2) kill -9
kill -s 9 15572
其中,-s 9 制定了傳遞給進程的信號是9,即強制、儘快終止進程。 15572則是上面ps查到的火狐(firefox)的PID。
簡單吧,但有個問題,進程少時ps還無所謂,進程多了,就會覺得痛苦了,無論是ps -ef 還是ps -aux,每次都要在一大串進程信息裏面查找到要殺的進程PID,看的眼都花了。
用法詳解:
kill -9, 這個強大和危險的命令迫使進程在運行時突然終止,進程在結束後不能自我清理。危害是導致系統資源無法正常釋放,一般不推薦使用,除非其他辦法都無效。
當使用此命令時,一定要通過ps -ef確認沒有剩下任何殭屍進程。只能通過終止父進程來消除殭屍進程。如果殭屍進程被init收養,問題就比較嚴重了。殺死init進程意味着關閉系統。
如果系統中有殭屍進程,並且其父進程是init,而且殭屍進程佔用了大量的系統資源,那麼就需要在某個時候重啓機器以清除進程表了。
2.1) 改進 1 —— grep
把ps的查詢結果通過管道傳給grep,查找包含特定字符串的進程。管道符“|”用來隔開兩個命令,管道符左邊命令的輸出會作爲管道符右邊命令的輸入。
例如: homer@ubuntu:~$ ps -aux | grep firefox
homer 15572 1.7 5.0 808644 103260 ? Sl 23:45 0:07 /usr/lib/firefox/firefox
homer 15735 0.0 0.0 13584 920 pts/0 S+ 23:52 0:00 grep --color=auto firefox
直接找到firefox進程PID, 輸入: kill -s 9 15572
2.2) 改進 2 —— pgrep
pgrep的p表明了這個命令是專門用於進程查詢的grep
例如: homer@ubuntu:~$ pgrep firefox
15572
2.3) 改進 3 —— pidof
pidof命令,表示 pid of xx,字面翻譯過來就是 xx的PID
例如: homer@ubuntu:~$ pidof firefox
15572
2.4) 改進4 —— grep + awk
grep firefox 列出firefox進程信息,並awk取第二個field,即PID
例如: homer@ubuntu:~$ ps -ef | grep firefox | grep -v grep | awk '{print $2}'
15572
2.5) 改進 5 —— kill + xargs
殺死進程的方式有以下幾種:
a) ps -ef | grep firefox | grep -v grep | awk '{print $2}' | xargs kill -s 9
b) pgrep firefox | xargs kill -s 9
c) pidof firefox | xargs kill -s 9
或
d) kill -s 9 `ps -ef | grep firefox | grep -v grep | awk '{print $2}'`
e) kill -s 9 `pgrep firefox`
f) kill -s 9 `pidof firefox`
2.6) 改進 6 —— pkill
pkill 類似於 pgrep, pkill 表示 pgrep+kill
例如: pkill firefox
3) killall
killall命令, 殺死同一進程組內的所有進程,其允許指定要終止的進程的名稱,而非PID
killall和pkill是相似的,不過如果給出的進程名不完整,killall會報錯。pkill或者pgrep只要給出進程名的一部分就可以終止進程。
homer@ubuntu:~$ killall firefo
firefo: no process found
homer@ubuntu:~$ killall firefox
homer@ubuntu:~$ killall -9 firefox
4) kill
殺死進程最安全的方法是單純使用kill命令,不加修飾符,不帶標誌。
例如: # kill -pid
註釋: 標準的kill命令,默認採用信號(signal)號是15,通常都能達到目的,終止有問題的進程,並把進程的資源釋放給系統。然而,如果進程啓動了子進程,只殺死父進程,子進程仍在運行,因此仍消耗資源。爲了防止這些所謂的“殭屍進程”,應確保在殺死父進程之前,先殺死其所有的子進程。
5) kill -l
例如: kill -l PID
-l 選項, 告訴kill命令用好像啓動進程的用戶已註銷的方式結束進程。當使用該選項時,kill命令也試圖殺死所留下的子進程。但這個命令也不是總能成功--或許仍然需要先手工殺死子進程,然後再殺死父進程。
6) kill -HUP
有時候只想簡單的停止和重啓進程。
例如: # kill -HUP PID
該命令讓Linux和緩的執行進程關閉,然後立即重啓。在配置應用程序的時候,這個命令很方便,在對配置文件修改後需要重啓進程時就可以執行此命令。
附錄:各種信號及其用途
Signal | Description | Signal number on Linux x86 |
---|---|---|
SIGABRT | Process aborted | 6 |
SIGALRM | Signal raised by alarm | 14 |
SIGBUS | Bus error: "access to undefined portion of memory object" | 7 |
SIGCHLD | Child process terminated, stopped (or continued*) | 17 |
SIGCONT | Continue if stopped | 18 |
SIGFPE | Floating point exception: "erroneous arithmetic operation" | 8 |
SIGHUP | Hangup | 1 |
SIGILL | Illegal instruction | 4 |
SIGINT | Interrupt | 2 |
SIGKILL | Kill (terminate immediately) | 9 |
SIGPIPE | Write to pipe with no one reading | 13 |
SIGQUIT | Quit and dump core | 3 |
SIGSEGV | Segmentation violation | 11 |
SIGSTOP | Stop executing temporarily | 19 |
SIGTERM | Termination (request to terminate) | 15 |
SIGTSTP | Terminal stop signal | 20 |
SIGTTIN | Background process attempting to read from tty ("in") | 21 |
SIGTTOU | Background process attempting to write to tty ("out") | 22 |
SIGUSR1 | User-defined 1 | 10 |
SIGUSR2 | User-defined 2 | 12 |
SIGPOLL | Pollable event | 29 |
SIGPROF | Profiling timer expired | 27 |
SIGSYS | Bad syscall | 31 |
SIGTRAP | Trace/breakpoint trap | 5 |
SIGURG | Urgent data available on socket | 23 |
SIGVTALRM | Signal raised by timer counting virtual time: "virtual timer expired" | 26 |
SIGXCPU | CPU time limit exceeded | 24 |
SIGXFSZ | File size limit exceeded | 25 |
grep GlobalHandlerExceptionResolver . -nr --exclude-dir=".svn" --binary-files=without-match