`kill -9` 和 `pkill`

kill -9pkill 是在类 Unix 系统中用来终止进程的两个不同命令,它们之间有以下主要区别:

  1. 信号不同

    • kill -9:发送 SIGKILL 信号给指定的进程。SIGKILL(信号值为 9)是一个强制杀死进程的信号,不能被进程捕获或忽略,进程将立即终止。
    • pkill:可以发送多种信号给进程,SIGKILL 只是其中之一。使用 pkill 时,可以通过 -s--signal 选项指定要发送的信号。
  2. 目标选择方式不同

    • kill -9:通常需要指定要杀死的进程的 PID(进程 ID),例如 kill -9 1234
    • pkill:通过进程名或其他属性来选择要杀死的进程,例如 pkill -9 nginx 会向所有名为 nginx 的进程发送 SIGKILL 信号。
  3. 使用范围

    • kill -9:只能杀死单个指定的进程。
    • pkill:可以杀死所有匹配特定条件的进程,这使得 pkill 更适合批量操作。
  4. 适用场景

    • kill -9:由于其强制杀死进程的特性,通常作为最后的手段使用,当其他信号(如 SIGTERM)无法终止进程时。
    • pkill:更适合需要同时终止多个相同名称或属性的进程的场景。
  5. 命令选项

    • kill -9:选项较少,主要用来发送 SIGKILL 信号。
    • pkill:有更多的选项,可以指定信号、匹配条件等。
  6. 安全性

    • kill -9:由于不能被进程捕获或忽略,使用时需要谨慎,以避免可能的数据丢失或不一致。
    • pkill:虽然也可以发送 SIGKILL,但由于其选择性更强,可以更精确地控制要杀死的进程集合。

总结来说,kill -9 是一个简单直接的命令,用于强制杀死指定的单个进程,而 pkill 提供了更多的灵活性和选择性,可以发送不同的信号并针对一组进程执行操作。在实际使用中,应根据具体需求和场景选择合适的命令。

pkill -help

Usage:
 pkill [options] <pattern>

Options:
 -<sig>, --signal <sig>    signal to send (either number or name)
 -e, --echo                display what is killed
 -c, --count               count of matching processes
 -f, --full                use full process name to match
 -g, --pgroup <PGID,...>   match listed process group IDs
 -G, --group <GID,...>     match real group IDs
 -n, --newest              select most recently started
 -o, --oldest              select least recently started
 -P, --parent <PPID,...>   match only child processes of the given parent
 -s, --session <SID,...>   match session IDs
 -t, --terminal <tty,...>  match by controlling terminal
 -u, --euid <ID,...>       match by effective IDs
 -U, --uid <ID,...>        match by real IDs
 -x, --exact               match exactly with the command name
 -F, --pidfile <file>      read PIDs from file
 -L, --logpidfile          fail if PID file is not locked
 --ns <PID>                match the processes that belong to the same
                           namespace as <pid>
 --nslist <ns,...>         list which namespaces will be considered for
                           the --ns option.
                           Available namespaces: ipc, mnt, net, pid, user, uts

 -h, --help     display this help and exit
 -V, --version  output version information and exit

For more details see pgrep(1).

KILL(1)                                                                             User Commands                                                                            KILL(1)

NAME
       kill - terminate a process

SYNOPSIS
       kill [-s signal|-p] [-q sigval] [-a] [--] pid...
       kill -l [signal]

DESCRIPTION
       The  command  kill sends the specified signal to the specified process or process group.  If no signal is specified, the TERM signal is sent.  The TERM signal will kill pro‐
       cesses which do not catch this signal.  For other processes, it may be necessary to use the KILL (9) signal, since this signal cannot be caught.

       Most modern shells have a builtin kill function, with a usage rather similar to that of the command described here.  The '-a' and '-p' options, and the possibility to  spec‐
       ify processes by command name are a local extension.

       If sig is 0, then no signal is sent, but error checking is still performed.

OPTIONS
       pid... Specify the list of processes that kill should signal.  Each pid can be one of five things:

              n      where n is larger than 0.  The process with pid n will be signaled.

              0      All processes in the current process group are signaled.

              -1     All processes with pid larger than 1 will be signaled.

              -n     where  n  is  larger  than  1.  All processes in process group n are signaled.  When an argument of the form '-n' is given, and it is meant to denote a process
                     group, either the signal must be specified first, or the argument must be preceded by a '--' option, otherwise it will be taken as the signal to send.

              commandname
                     All processes invoked using that name will be signaled.

       -s, --signal signal
              Specify the signal to send.  The signal may be given as a signal name or number.

       -l, --list [signal]
              Print a list of signal names, or convert signal given as argument to a name.  The signals are found in /usr/include/linux/signal.h

       -L, --table
              Similar to -l, but will print signal names and their corresponding numbers.

       -a, --all
              Do not restrict the commandname-to-pid conversion to processes with the same uid as the present process.

       -p, --pid
              Specify that kill should only print the process id (pid) of the named processes, and not send any signals.

       -q, --queue sigval
              Use sigqueue(2) rather than kill(2) and the sigval argument is used to specify an integer to be sent with the signal.  If the receiving process has installed  a  han‐
              dler for this signal using the SA_SIGINFO flag to sigaction(2), then it can obtain this data via the si_value field of the siginfo_t structure.
相关推荐
Hacker_LaoYi37 分钟前
【渗透技术总结】SQL手工注入总结
数据库·sql
岁月变迁呀39 分钟前
Redis梳理
数据库·redis·缓存
独行soc39 分钟前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍06-基于子查询的SQL注入(Subquery-Based SQL Injection)
数据库·sql·安全·web安全·漏洞挖掘·hw
你的微笑,乱了夏天1 小时前
linux centos 7 安装 mongodb7
数据库·mongodb
远游客07131 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
马甲是掉不了一点的<.<1 小时前
本地电脑使用命令行上传文件至远程服务器
linux·scp·cmd·远程文件上传
jingyu飞鸟1 小时前
centos-stream9系统安装docker
linux·docker·centos
工业甲酰苯胺1 小时前
分布式系统架构:服务容错
数据库·架构
超爱吃士力架2 小时前
邀请逻辑
java·linux·后端
独行soc2 小时前
#渗透测试#漏洞挖掘#红蓝攻防#护网#sql注入介绍08-基于时间延迟的SQL注入(Time-Based SQL Injection)
数据库·sql·安全·渗透测试·漏洞挖掘