发表于: lnmp | 作者: | 日期: 2009/7/12 02:07

当需要中断一个前台进程的时候,通常是使用< Ctrl+c >组合键;但是对于一个后台进程恐怕就不是一个组合键所能解决的了,这时就必须求助于kill命令。该命令可以终止后台进程。至于终止后台进程的原因很多,或许是该进程占用的CPU时间过多;或许是该进程已经挂死。总之这种情况是经常发生的。

kill命令是通过向进程发送指定的信号来结束进程的。如果没有指定发送信号,那么默认值为TERM信号。TERM信号将终止所有不能捕获该信号的进程。至于那些可以捕获该信号的进程可能就需要使用kill(9)信号了,该信号是不能被捕捉的。

kill命令的语法格式很简单,大致有以下两种方式:

kill [-s 信号 | -p ] [ -a ] 进程号 …

kill -l [信号]

-s 指定需要送出的信号。既可以是信号名也可以对应数字。

-p 指定kill命令只是显示进程的pid,并不真正送出结束信号。

-l 显示信号名称列表,这也可以在/usr/include/linux/signal.h文件中找到。

kill命令的使用

下面看看该命令的使用。

例:在执行一条find指令时由于时间过长,决定终止该进程。

首先应该使用ps命令来查看该进程对应的PID,键入ps,显示如下:

PID TTY TIME COMMAND
285 1 00:00:00 -bash
287 3 00:00:00 -bash
289 5 00:00:00 /sbin/mingetty tty5
290 6 00:00:00 /sbin/mingetty tty6
312 3 00:00:00 telnet bbs3
341 4 00:00:00 /sbin/mingetty tty4
345 1 00:00:00 find / -name foxy.jpg
348 1 00:00:00 ps

可以看到该进程对应的PID是345,现在使用kill命令来终止该进程。键入:

# kill 345

再用ps命令查看,就可以看到,find进程已经被杀掉了。

例:杀掉进程11721

# ps PID TTY TIME COMMAND
11668 p1 00:00:00 -tcsh
11721 p1 00:00:00 cat
11737 p1 00:00:00 ps
# kill 11721
[1] Terminated cat
#

有时候可能会遇到这样的情况,某个进程已经挂死或闲置,使用kill命令却杀不掉。这时候就必须发送信号9,强行关闭此进程。当然这种“野蛮”的方法很可能会导致打开的文件出现错误或者数据丢失之类的错误。所以不到万不得已不要使用强制结束的办法。如果连信号9都不响应,那恐怕就只有重新启动计算机了。


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

kill Command

Purpose

Sends a signal to running processes.

Syntax

To Send Signal to Processes

kill [ -s { SignalName | SignalNumber } ] ProcessID …

kill [ – SignalName | – SignalNumber ] ProcessID …

To List Signal Names

kill -l [ ExitStatus ]

Description

The kill command sends a signal (by default, the SIGTERM signal) to a running process. This default action normally stops
processes. If you want to stop a process, specify the process ID (PID) in the ProcessID variable. The shell reports the PID
of each process that is running in the background (unless you start more than one process in a pipeline, in which case the
shell reports the number of the last process). You can also use the ps command to find the process ID number of commands.

A root user can stop any process with the kill command. If you are not a root user, you must have initiated the process you
want to stop.

SignalName is recognized in a case-independent fashion, without the SIG prefix.

If the specified SignalNumber is 0, the kill command checks the validity of the specified PID.

Flags

-s{SignalName | SignalNumber}
Specifies the signal as a signal number or a signal name, such as -9 or KILL for the SIGKILL signal.
-SignalName
Specifies a signal name, such as SIGHUP.
-SignalNumber
Specifies a signal number.

Note: To specify the negative PID with the default signal in this syntax, you must specify – – as a signal. Otherwise
the first operand is interpreted as a SignalNumber.

ProcessID
Specifies a decimal integer representing a process or process group to be signaled. If PID is a positive value, the
kill command sends the process whose process ID is equal to the PID. If the PID value is 0, the kill command sends the
signal to all processes having a process group ID equal to the process group ID of the sender. The signal is not sent
to processes with a PID of 0 or 1. If the PID is -1, the kill command sends the signal to all processes owned by the
effective user of the sender. The signal is not sent to processes with a PID of 0 or 1. If it is a negative number but
not -1, the kill command sends the signal to all processes that have a process group ID equal to the absolute value of
the PID.
-l
Lists all signal names supported by the implementation
-lExitStatus
Lists signal names stripped of the common SIG prefix. If ExitStatus is an decimal integer value, the signal name
corresponding to that signal is displayed. If ExitStatus is a value of the exit status corresponding to a process that

was terminated by a signal, the signal name corresponding to the signal that terminated the process is displayed.

Exit Status

This command returns the following exit values:
0
At least one matching process was found for each ProcessID operand, and the specified signal was successfully
processed for at least one matching process.
>0
An error occurred.

Examples
1 To stop a given process, enter:

kill 1095

This stops process 1095 by sending it the default SIGTERM signal. Note that process 1095 might not actually stop if it
has made special arrangements to ignore or override the SIGTERM signal.
2 To stop several processes that ignore the default signal, enter:

kill -kill 2098 1569

This sends signal 9, the SIGKILL signal, to processes 2098 and 1569. The SIGKILL signal is a special signal that
normally cannot be ignored or overridden.
3 To stop all of your processes and log yourself off, enter:

kill -kill 0

This sends signal 9, the SIGKILL signal, to all processes having a process group ID equal to the senders process group
ID. Because the shell cannot ignore the SIGKILL signal, this also stops the login shell and logs you off.
4 To stop all processes that you own, enter:

kill -9 -1

This sends signal 9, the SIGKILL signal, to all processes owned by the effective user, even those started at other
work stations and that belong to other process groups. If a listing that you requested is being printed, it is also
stopped.
5 To send a different signal code to a process, enter:

kill -USR1 1103

The name of the kill command is misleading because many signals, including SIGUSR1, do not stop processes. The action
taken on SIGUSR1 is defined by the particular application you are running.

Note: To send signal 15, the SIGTERM signal with this form of the kill command, you must explicitly specify -15 or
TERM.

Files

/usr/include/sys/signal.h
Specifies signal names.

Related Information

The csh command, ksh command, ps command, sh command.

The kill subroutine, sigaction subroutine.

上半部分引自:http://tech.ddvip.com/2007-03/117338439220432.html

: https://blog.darkmi.com/2009/07/12/889.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.