【Linux】命令 - kill

Posted by 西维蜀黍 on 2021-02-18, Last Modified on 2024-08-28

kill

Sends a signal to a process, usually related to stopping the process.

All signals except for SIGKILL and SIGSTOP can be intercepted by the process to perform a clean exit.

Usage

  • Alternate signals may be specified in three ways: -9, -SIGKILL or -KILL.
  • Negative PID values may be used to choose whole process groups; see the PGID column in ps command output.
  • A PID of -1 is special; it indicates all processes except the kill process itself and init.

Show Signal Info

# List signal names in a nice table.
$ kill -L
 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX

# -l, --list [signal]
# List signal names.  This option has optional argument,  which will convert signal number to signal name, or other way round.
$ kill -l 11
HUP

[Default] SIGTERM - 优雅杀掉指定进程

# Terminate a program using the default SIGTERM (terminate) signal:
$ kill [process_id]

# Terminate a background job:
$ kill %[job_id]

如果通过 kill 7703 结束一个进程,它的stdout中会有Terminated

$ python sw.py
Terminated

当使用 kill 杀掉进程时,可以通过 strace 看到进程收到的 signal:

# 发送 SIGTERM 以结束这个进程, by `kill 114469`
$ strace -ttp 114469
...
12:12:15.227492 futex(0x2e1c348, FUTEX_WAIT_PRIVATE, 0, NULL) = ? ERESTARTSYS (To be restarted if SA_RESTART is set)
12:12:55.291152 --- SIGTERM {si_signo=SIGTERM, si_code=SI_USER, si_pid=114561, si_uid=0} ---
12:12:55.291230 rt_sigprocmask(SIG_UNBLOCK, [TERM], NULL, 8) = 0
12:12:55.291319 getpid()                = 114469
12:12:55.291363 gettid()                = 114469
12:12:55.291402 tgkill(114469, 114469, SIGTERM) = 0
12:12:55.291760 --- SIGTERM {si_signo=SIGTERM, si_code=SI_TKILL, si_pid=114469, si_uid=0} ---
12:12:55.291804 rt_sigaction(SIGTERM, {SIG_DFL, ~[], SA_RESTORER|SA_STACK|SA_RESTART|SA_SIGINFO, 0x7f7b485d8390}, NULL, 8) = 0
12:12:55.292002 rt_sigprocmask(SIG_UNBLOCK, [TERM], NULL, 8) = 0
12:12:55.292118 getpid()                = 114469
12:12:55.292193 gettid()                = 114469
12:12:55.292260 tgkill(114469, 114469, SIGTERM) = 0
12:12:55.292332 --- SIGTERM {si_signo=SIGTERM, si_code=SI_TKILL, si_pid=114469, si_uid=0} ---
12:12:55.295492 +++ killed by SIGTERM +++

SIGHUP

# Terminate a program using the SIGHUP (hang up) signal. Many daemons will reload instead of terminating:
$ kill -1 [process_id]
$ kill -HUP [process_id]

如果通过 kill -1 7717 结束一个进程,它的stdout中会有Hangup

$ python sw.py
Hangup

SIGINT - 优雅杀掉指定进程 - Ctrl + C

# Terminate a program using the SIGINT (interrupt) signal. This is typically initiated by the user pressing Ctrl + C:
$ kill -2 [process_id]
$ kill -INT [process_id]

SIGKILL - 强制杀掉指定进程

# Signal the operating system to immediately terminate a program (which gets no chance to capture the sinal:
$ kill -9 [process_id]
$ kill -KILL [process_id]

当使用 kill -9 杀掉进程时,可以通过 strace 看到进程收到的 signal:

# 发送 SIGKILL 以结束这个进程, by `kill -9 115677`
$ strace -ttp 115677
strace: Process 115677 attached
12:25:00.762968 futex(0x2e47540, FUTEX_WAIT_PRIVATE, 0, NULL <unfinished ...>
12:25:13.831309 +++ killed by SIGKILL +++

SIGSTOP

# Signal the operating system to pause a program until a SIGCONT ("continue") signal is received:
$ kill -17 process_id
$ kill -STOP process_id

这里,我执行 kill -17 7839后,进程并没有什么影响(仍然继续执行):

$ python sw.py
111
111
...

我不知道为什么。

SIGUSR1

# Send a SIGUSR1 signal to all processes with the given GID (group id):
$ kill -SIGUSR1 -group_id

Misc

同时杀死多个进程

pkill

# Kill all processes which match:
$ pkill "process_name"

# Kill all processes which match their full command instead of just the process name:
$ pkill -f "command_name"

# Force kill matching processes (can't be blocked):
$ pkill -9 "process_name"

# Send SIGUSR1 signal to processes which match:
$ pkill -USR1 "process_name"

# Kill the main firefox process to close the browser:
$ pkill --oldest "firefox"

kill

for KILLPID in `ps ax | grep 'my_pattern' | grep -v "grep" | awk ' { print $1;}'`; do 
  kill -9 $KILLPID;
done

killall

# Terminate a process using the default SIGTERM (terminate) signal:
$ killall process_name

# List available signal names (to be used without the 'SIG' prefix):
$ killall --list

# Interactively ask for confirmation before termination:
$ killall -i process_name

# Terminate a process using the SIGINT (interrupt) signal, which is the same signal sent by pressing Ctrl + C:
$ killall -INT process_name

# Force kill a process:
$ killall -KILL process_name

Reference

  • man kill