Linux 之 killall 命令
.. _linux-beginner-killall:
.. note::
及时当勉励,岁月不待人。
陶渊明《杂诗·人生无根蒂》
命令概述
在Linux系统中,有许多命令可用于进程管理和控制。
其中一个常用的命令是killall
,它允许用户通过进程名字来终止运行中的进程。
官方定义为:
killall – kill processes by name
killall
命令用于向操作系统发送信号以终止指定进程。与kill
命令不同,killall
根据进程名字而不是进程ID来选择要终止的进程。这对于同时终止多个同名进程非常有用。
超级管理员可以kill掉任何进程。
基本语法
killall
命令的基本语法如下:
1 | $ killall [选项] 进程名 |
可以使用以下选项对killall
命令进行调整:
-i
:交互式模式,要求用户确认终止每个进程。-e
:精确匹配进程名,不匹配进程名的任何子串。-s
:指定要发送的信号类型,如-s HUP
。-v
:显示详细的终止进程的输出。
使用示例
终止单个进程
要终止单个进程,可以使用以下命令:
1 | $ killall 进程名 |
比如:
1 | $ killall firefox |
这将终止所有名为firefox
的进程。
终止多个进程
要同时终止多个同名进程,可以使用以下命令:
1 | $ killall -r 进程名 |
示例:
1 | $ killall -r chrome |
这将终止所有以chrome
为名的进程,包括chrome
和chromium
等等。
交互式模式
使用-i
选项可以在终止每个进程之前要求用户确认。示例:
1 | $ killall -i firefox |
在执行此命令时,系统将逐个显示要终止的进程,并要求用户确认是否继续,这个对于不确定是否一定中止的优点用哟。
指定信号类型
可以使用-s
选项来指定要发送的信号类型。示例:
1 | $ killall -s HUP nginx |
这将向所有名为nginx
的进程发送HUP
信号,以重新加载配置。
综上
killall
命令是一个强大的进程管理工具,可帮助用户终止指定名称的进程。它简化了终止多个同名进程的操作,并提供了一些有用的选项,如交互式模式和指定信号类型。在日常的系统管理和故障排除中,killall
是一个重要的工具,
所有可用的信号
使用 kill -l
命令列出所有可用信号。
1 | $ kill -l |
其中最常用的信号为:
- 1 (HUP):重新加载进程。
- 9 (KILL):杀死一个进程。
- 15 (TERM):正常停止一个进程。
几个实例
杀死进程
1 | $ kill 12345 |
强制杀死进程
1 | $ kill -KILL 123456 |
那么如何kill某个用户的所有进程呢,比如用户为user,可以通过下面的命令执行:
1 | $ kill -9 $(ps -ef | grep user) |
SYNOPSIS
killall [-delmsvqz] [-help] [-I] [-u user] [-t tty] [-c procname] [-SIGNAL] [procname …]
The options are as follows:
-d Be more verbose about what will be done, but do not send any signal. The total number of user processes and the real user ID is shown. A list of the processes that
will be sent the signal will be printed, or a message indicating that no matching processes have been found.
-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.
-I Request confirmation before attempting to signal each process.
-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.
-v Be verbose about what will be done.
-s Same as -v, 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.
-c procname Limit potentially matching processes to those matching the specified procname.
-q Suppress error message if no processes are matched.
-z Do not skip zombies. This should not have any effect except to print a few error messages if there are zombie processes that match the specified pattern.
ALL PROCESSES
Sending a signal to all processes with the given UID 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
IMPLEMENTATION NOTES
This FreeBSD implementation of killall has completely different semantics as compared to the traditional UNIX System V behavior of killall. The latter will kill all processes that the
current user is able to kill, and is intended to be used by the system shutdown process only.
EXIT STATUS
The killall utility exits 0 if some processes have been found and signalled successfully. Otherwise, a status of 1 will be returned.
EXAMPLES
Send SIGTERM to all firefox processes:
killall firefox
Send SIGTERM to firefox processes belonging to USER:
killall -u ${USER} firefox
Stop all firefox processes:
killall -SIGSTOP firefox
Resume firefox processes:
killall -SIGCONT firefox
Show what would be done to firefox processes, but do not actually signal them:
killall -s firefox
Send SIGTERM to all processes matching provided pattern (like vim and vimdiff):
killall -m 'vim*'
DIAGNOSTICS
Diagnostic messages will only be printed if the -d flag is used.
SEE ALSO
kill(1), pkill(1), sysctl(3)