回到手册索引

命令用途

kill 是 Linux/Unix 系统中用于向进程发送信号的命令,默认发送 SIGTERM(终止信号),常用于终止或控制进程的运行状态。

常用用法示例

  1. 终止指定PID的进程

    1
    kill 1234  

    向进程ID为1234的进程发送默认信号SIGTERM(15),请求其正常退出。若进程响应,则无输出;若失败会提示错误(如权限不足或进程不存在)。

  2. 强制终止进程

    1
    kill -9 1234  

    发送SIGKILL信号(编号9),强制立即终止PID为1234的进程。无输出表示操作成功。

  3. 终止进程并显示操作结果

    1
    2
    kill -v 1234  
    kill: 1234: No such process

    -v参数会显示操作结果,若进程不存在则提示错误。

  4. 向进程发送挂起信号

    1
    kill -HUP 1234  

    发送SIGHUP信号(1),常用于通知进程重新加载配置文件(如Nginx)。

  5. 终止进程组

    1
    kill -9 -1234  

    向进程组ID为1234的所有进程发送SIGKILL,负号表示操作进程组。

  6. 列出所有支持的信号名称

    1
    2
    3
    kill -l  
    1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
    ...

    显示系统支持的信号列表及其编号。

  7. 使用信号名称发送信号

    1
    kill -s SIGUSR1 1234  

    向PID为1234的进程发送自定义信号SIGUSR1,常用于进程间通信。

  8. 批量终止多个进程

    1
    kill -9 1234 5678 9012  

    同时强制终止PID为1234、5678、9012的多个进程。

常用参数选项

  • -9, -SIGKILL
    强制立即终止进程,进程无法捕获或忽略此信号。

  • -15, -SIGTERM
    默认信号,请求进程正常终止(允许进程清理资源)。

  • -l, –list
    列出所有支持的信号名称及编号。

  • -s, –signal
    指定信号名称或编号(如-s SIGINT或-s 2)。

  • -HUP, -1
    发送挂起信号,常用于通知守护进程重新加载配置。

  • -INT, -2
    发送中断信号(等同于Ctrl+C),通常用于终止前台进程。

  • -QUIT, -3
    发送退出信号(等同于Ctrl+\),终止进程并生成核心转储文件。

  • -USER1, -USR1
    发送用户自定义信号1,可用于触发进程自定义操作(如日志轮转)。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
PROLOG 

This manual page is part of the POSIX Programmer's Manual. The
Linux implementation of this interface may differ (consult the
corresponding Linux manual page for details of Linux behavior), or
the interface may not be implemented on Linux.

NAME

kill — terminate or signal processes

SYNOPSIS

kill -s signal_name pid...

kill -l [exit_status]

kill [-signal_name] pid...

kill [-signal_number] pid...

DESCRIPTION

The kill utility shall send a signal to the process or processes
specified by each pid operand.

For each pid operand, the kill utility shall perform actions
equivalent to the kill() function defined in the System Interfaces
volume of POSIX.1‐2017 called with the following arguments:

* The value of the pid operand shall be used as the pid
argument.

* The sig argument is the value specified by the -s option,
-signal_number option, or the -signal_name option, or by
SIGTERM, if none of these options is specified.

OPTIONS

The kill utility shall conform to the Base Definitions volume of
POSIX.1‐2017, Section 12.2, Utility Syntax Guidelines, except that
in the last two SYNOPSIS forms, the -signal_number and
-signal_name options are usually more than a single character.

The following options shall be supported:

-l (The letter ell.) Write all values of signal_name
supported by the implementation, if no operand is given.
If an exit_status operand is given and it is a value of
the '?' shell special parameter (see Section 2.5.2,
Special Parameters and wait) corresponding to a process
that was terminated by a signal, the signal_name
corresponding to the signal that terminated the process
shall be written. If an exit_status operand is given and
it is the unsigned decimal integer value of a signal
number, the signal_name (the symbolic constant name
without the SIG prefix defined in the Base Definitions
volume of POSIX.1‐2017) corresponding to that signal
shall be written. Otherwise, the results are
unspecified.

-s signal_name
Specify the signal to send, using one of the symbolic
names defined in the <signal.h> header. Values of
signal_name shall be recognized in a case-independent
fashion, without the SIG prefix. In addition, the
symbolic name 0 shall be recognized, representing the
signal value zero. The corresponding signal shall be
sent instead of SIGTERM.

-signal_name
Equivalent to -s signal_name.

-signal_number
Specify a non-negative decimal integer, signal_number,
representing the signal to be used instead of SIGTERM,
as the sig argument in the effective call to kill().
The correspondence between integer values and the sig
value used is shown in the following list.

The effects of specifying any signal_number other than
those listed below are undefined.

0 0

1 SIGHUP

2 SIGINT

3 SIGQUIT

6 SIGABRT

9 SIGKILL

14 SIGALRM

15 SIGTERM

If the first argument is a negative integer, it shall be
interpreted as a -signal_number option, not as a
negative pid operand specifying a process group.

OPERANDS

The following operands shall be supported:

pid One of the following:

1. A decimal integer specifying a process or process
group to be signaled. The process or processes
selected by positive, negative, and zero values of
the pid operand shall be as described for the kill()
function. If process number 0 is specified, all
processes in the current process group shall be
signaled. For the effects of negative pid numbers,
see the kill() function defined in the System
Interfaces volume of POSIX.1‐2017. If the first pid
operand is negative, it should be preceded by "--"
to keep it from being interpreted as an option.

2. A job control job ID (see the Base Definitions
volume of POSIX.1‐2017, Section 3.204, Job Control
Job ID) that identifies a background process group
to be signaled. The job control job ID notation is
applicable only for invocations of kill in the
current shell execution environment; see Section
2.12, Shell Execution Environment.

exit_status
A decimal integer specifying a signal number or the exit
status of a process terminated by a signal.

STDIN

Not used.

INPUT FILES

None.

ENVIRONMENT VARIABLES

The following environment variables shall affect the execution of
kill:

LANG Provide a default value for the internationalization
variables that are unset or null. (See the Base
Definitions volume of POSIX.1‐2017, Section 8.2,
Internationalization Variables for the precedence of
internationalization variables used to determine the
values of locale categories.)

LC_ALL If set to a non-empty string value, override the values
of all the other internationalization variables.

LC_CTYPE Determine the locale for the interpretation of sequences
of bytes of text data as characters (for example,
single-byte as opposed to multi-byte characters in
arguments).

LC_MESSAGES
Determine the locale that should be used to affect the
format and contents of diagnostic messages written to
standard error.

NLSPATH Determine the location of message catalogs for the
processing of LC_MESSAGES.

ASYNCHRONOUS EVENTS

Default.

STDOUT

When the -l option is not specified, the standard output shall not
be used.

When the -l option is specified, the symbolic name of each signal
shall be written in the following format:

"%s%c", <signal_name>, <separator>

where the <signal_name> is in uppercase, without the SIG prefix,
and the <separator> shall be either a <newline> or a <space>. For
the last signal written, <separator> shall be a <newline>.

When both the -l option and exit_status operand are specified, the
symbolic name of the corresponding signal shall be written in the
following format:

"%s\n", <signal_name>

STDERR

The standard error shall be used only for diagnostic messages.

OUTPUT FILES

None.

EXTENDED DESCRIPTION

None.

EXIT STATUS

The following exit values shall be returned:

0 At least one matching process was found for each pid
operand, and the specified signal was successfully processed
for at least one matching process.

>0 An error occurred.

CONSEQUENCES OF ERRORS

Default.

The following sections are informative.

APPLICATION USAGE

Process numbers can be found by using ps.

The job control job ID notation is not required to work as
expected when kill is operating in its own utility execution
environment. In either of the following examples:

nohup kill %1 &
system("kill %1");

the kill operates in a different environment and does not share
the shell's understanding of job numbers.

EXAMPLES

Any of the commands:

kill -9 100 -165
kill -s kill 100 -165
kill -s KILL 100 -165

sends the SIGKILL signal to the process whose process ID is 100
and to all processes whose process group ID is 165, assuming the
sending process has permission to send that signal to the
specified processes, and that they exist.

The System Interfaces volume of POSIX.1‐2017 and this volume of
POSIX.1‐2017 do not require specific signal numbers for any
signal_names. Even the -signal_number option provides symbolic
(although numeric) names for signals. If a process is terminated
by a signal, its exit status indicates the signal that killed it,
but the exact values are not specified. The kill -l option,
however, can be used to map decimal signal numbers and exit status
values into the name of a signal. The following example reports
the status of a terminated job:

job
stat=$?
if [ $stat -eq 0 ]
then
echo job completed successfully.
elif [ $stat -gt 128 ]
then
echo job terminated by signal SIG$(kill -l $stat).
else
echo job terminated with error code $stat.
fi

To send the default signal to a process group (say 123), an
application should use a command similar to one of the following:

kill -TERM -123
kill -- -123

RATIONALE

The -l option originated from the C shell, and is also implemented
in the KornShell. The C shell output can consist of multiple
output lines because the signal names do not always fit on a
single line on some terminal screens. The KornShell output also
included the implementation-defined signal numbers and was
considered by the standard developers to be too difficult for
scripts to parse conveniently. The specified output format is
intended not only to accommodate the historical C shell output,
but also to permit an entirely vertical or entirely horizontal
listing on systems for which this is appropriate.

An early proposal invented the name SIGNULL as a signal_name for
signal 0 (used by the System Interfaces volume of POSIX.1‐2017 to
test for the existence of a process without sending it a signal).
Since the signal_name 0 can be used in this case unambiguously,
SIGNULL has been removed.

An early proposal also required symbolic signal_names to be
recognized with or without the SIG prefix. Historical versions of
kill have not written the SIG prefix for the -l option and have
not recognized the SIG prefix on signal_names. Since neither
applications portability nor ease-of-use would be improved by
requiring this extension, it is no longer required.

To avoid an ambiguity of an initial negative number argument
specifying either a signal number or a process group, POSIX.1‐2008
mandates that it is always considered the former by
implementations that support the XSI option. It also requires that
conforming applications always use the "--" options terminator
argument when specifying a process group, unless an option is also
specified.

The -s option was added in response to international interest in
providing some form of kill that meets the Utility Syntax
Guidelines.

The job control job ID notation is not required to work as
expected when kill is operating in its own utility execution
environment. In either of the following examples:

nohup kill %1 &
system("kill %1");

the kill operates in a different environment and does not
understand how the shell has managed its job numbers.

FUTURE DIRECTIONS

None.

SEE ALSO

Chapter 2, Shell Command Language, ps(1p), wait(1p)

The Base Definitions volume of POSIX.1‐2017, Section 3.204, Job
Control Job ID, Chapter 8, Environment Variables, Section 12.2,
Utility Syntax Guidelines, signal.h(0p)

The System Interfaces volume of POSIX.1‐2017, kill(3p)

COPYRIGHT

Portions of this text are reprinted and reproduced in electronic
form from IEEE Std 1003.1-2017, Standard for Information
Technology -- Portable Operating System Interface (POSIX), The
Open Group Base Specifications Issue 7, 2018 Edition, Copyright
(C) 2018 by the Institute of Electrical and Electronics Engineers,
Inc and The Open Group. In the event of any discrepancy between
this version and the original IEEE and The Open Group Standard,
the original IEEE and The Open Group Standard is the referee
document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page
are most likely to have been introduced during the conversion of
the source files to man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .