Linux小推车

MacOS 中使用 killall & kill

2018-12-18  本文已影响90人  枫林风雨

前言:终止一个前台进程可以使用Ctrl+C组合键,但是结束一个后台进程就须用 kill 命令来实现。kill 命令是通过向进程发送指定的信号来结束相应进程的。在默认情况下,采用编号为 15 的TERM信号。TERM 信号将终止所有不能捕获该信号的进程。对于那些可以捕获该信号的进程就要用编号为 9 的 kill 信号,强行“杀掉”该进程。

killall 杀死指定名字的进程

关闭 iTunes 程序(需要注意区分大小写): killall iTunes

killall 参数解释
man killall 读取官方说明:

因为网上查到的资料都比较旧,内容也千篇一律,有些命令都已经淘汰了,所以我直接晒出最新官方说明来讲。

NAME
     killall -- kill processes by name
SYNOPSIS
     killall [-delmsvz] [-help] [-u user] [-t tty] [-c procname] [-SIGNAL]
             [procname ...]
    DESCRIPTION
     The killall utility kills processes selected by name, as opposed to the
     selection by pid as done by kill(1).  By default, it will send a TERM
     signal to all processes with a real UID identical to the caller of
     killall that match the name procname.  The super-user is allowed to kill
     any process.

The options are as follows:
     -v          Be more verbose about what will be done.
     -e          Use the effective user ID instead of the (default) real
                 user ID for matching processes specified with the -u option.
     -help       Give a help on the command usage and exit.
     -l          List the names of the available signals and exit, like in kill(1).
     -m          Match the argument procname as a (case sensitive) regular expression 
                 against the names of processes found.
                 CAUTION!  This is dangerous, a single dot will match
                 any process running under the real UID of the caller.
     -s          Show only what would be done, but do not send any signal.
     -d          Print detailed information about the processes matched, but do not send any signal.
    -SIGNAL     Send a different signal instead of the default TERM.
                The signal may be specified either as a name (with or without a leading SIG), or numerically.

    -u user     Limit potentially matching processes to those belonging to the specified user.
    -t tty      Limit potentially matching processes to those running on the specified tty.

ALL PROCESSES
     Sending a signal to all processes with uid XYZ is already supported by
     kill(1).  So use kill(1) for this job (e.g. $ kill -TERM -1 or as root $
     echo kill -TERM -1 | su -m <user>)

EXIT STATUS
     The killall command will respond with a short usage message and exit with
     a status of 2 in case of a command error.  A status of 1 will be returned
     if either no matching process has been found or not all processes have
     been signalled successfully.  Otherwise, a status of 0 will be returned.

DIAGNOSTICS
     Diagnostic messages will only be printed if requested by -d options.

SEE ALSO
     kill(1), sysctl(3)

  1. real user id :真实用户ID,发起执行 process 的用户或进程的ID,是不会变的
  2. effective user ID:有效用户ID(判定一个进程是否对于某个文件有权限的时候要验证的ID),决定了进程访问文件的权限,一般情况下是与real user id是相同的,但是是可以改变的
  3. saved set-user-id: 当其他用户运行这个程序时,如果设置了set-user-id位,那么其他用户就可以像 real user 一样运行该程序了(这里主要是指对文件的访问权)
    user IDs截图说明.png

使用实例:把所有的登录后的shell给杀掉


kill 杀死指定PID的进程

先通过 ps/pidof/pstree/top等命令获取进程 pid,再执行 kill :kill -2 533
关闭多个进程:kill 142 157

kill 参数解释
man kill 读取官方说明:

kill 终止一个进程,或者给一个进程发送信号。

NAME
     kill -- terminate or signal a process
SYNOPSIS
     kill [-s signal_name] pid ...
     kill -l [exit_status]
     kill -signal_name pid ...
     kill -signal_number pid ...

DESCRIPTION
     The kill utility sends a signal to the processes specified by the pid operands.

     Only the super-user may send signals to other users' processes.

     The options are as follows:
     -s signal_name
             A symbolic signal name specifying the signal to be sent instead of
             the default TERM.
     -l [exit_status]
             If no operand is given, list the signal names; otherwise, write the
             signal name corresponding to exit_status.
     -signal_name
             A symbolic signal name specifying the signal to be sent instead of
             the default TERM.
     -signal_number
             A non-negative decimal integer, specifying the signal to be sent
             instead of the default TERM.

     The following PIDs have special meanings:
     -1      If superuser, broadcast the signal to all processes; otherwise
             broadcast to all processes belonging to the user.
             
     Some of the more commonly used signals:
     1       HUP (hang up)
     2       INT (interrupt)
     3       QUIT (quit)
     6       ABRT (abort)
     9       KILL (non-catchable, non-ignorable kill)
     14      ALRM (alarm clock)
     15      TERM (software termination signal)

     Some shells may provide a builtin kill command which is similar or identi-
     cal to this utility.  Consult the builtin(1) manual page.

EXIT STATUS
     The kill utility exits 0 on success, and >0 if an error occurs.
EXAMPLES
     Terminate the processes with PIDs 142 and 157:
           kill 142 157
     Send the hangup signal (SIGHUP) to the process with PID 507:
           kill -s HUP 507
SEE ALSO
     builtin(1), csh(1), killall(1), ps(1), sh(1), kill(2), sigaction(2)
kill -l 列出信号对照表(signal_number 和 signal_name)
 1) SIGHUP   2) SIGINT   3) SIGQUIT  4) SIGILL
 5) SIGTRAP  6) SIGABRT  7) SIGEMT   8) SIGFPE
 9) SIGKILL 10) SIGBUS  11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGURG
17) SIGSTOP 18) SIGTSTP 19) SIGCONT 20) SIGCHLD
21) SIGTTIN 22) SIGTTOU 23) SIGIO   24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM   27) SIGPROF 28) SIGWINCH
29) SIGINFO 30) SIGUSR1 31) SIGUSR2 
只有第9种信号(SIGKILL)才可以无条件终止进程,其他信号进程都有权利忽略。 下面是常用的信号:
HUP     1    终端断线
INT     2    中断(同 Ctrl + C)
QUIT    3    退出(同 Ctrl + \)
KILL    9    强制终止
TERM   15    终止
CONT   18    继续(与STOP相反, fg/bg命令)
STOP   19    暂停(同 Ctrl + Z)
需要注意的点:
  1. 用kill向这些进程发送信号时,必须是这些进程的主人。如果试图撤销一个没有权限撤销的进程或撤销一个不存在的进程,就会得到一个错误信息。
  2. 当kill成功地发送了信号后,shell 会在屏幕上显示出进程的终止信息。有时这个信息不会马上显示,只有当按下 Enter 键使 shell 的命令提示符再次出现时,才会显示出来。
  3. 信号使进程强行终止,这常会带来一些副作用,如数据丢失或者终端无法恢复到正常状态。发送信号时必须小心,只有在万不得已时,才用kill信号(9),因为进程不能首先捕获它。要撤销所有的后台作业,可以输入kill 0。因为有些在后台运行的命令会启动多个进程,跟踪并找到所有要杀掉的进程的PID是件很麻烦的事。这时,使用kill 0来终止所有由当前shell启动的进程,是个有效的方法。
上一篇 下一篇

猜你喜欢

热点阅读