当前位置: 首页 > news >正文

`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 -helpUsage: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 samenamespace as <pid>--nslist <ns,...>         list which namespaces will be considered forthe --ns option.Available namespaces: ipc, mnt, net, pid, user, uts-h, --help     display this help and exit-V, --version  output version information and exitFor more details see pgrep(1).
KILL(1)                                                                             User Commands                                                                            KILL(1)NAMEkill - terminate a processSYNOPSISkill [-s signal|-p] [-q sigval] [-a] [--] pid...kill -l [signal]DESCRIPTIONThe  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.OPTIONSpid... 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 processgroup, 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.commandnameAll processes invoked using that name will be signaled.-s, --signal signalSpecify 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, --tableSimilar to -l, but will print signal names and their corresponding numbers.-a, --allDo not restrict the commandname-to-pid conversion to processes with the same uid as the present process.-p, --pidSpecify that kill should only print the process id (pid) of the named processes, and not send any signals.-q, --queue sigvalUse 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.

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 31集-33集【求助】AIGC返回的对话内容文字转语音失败-《MCU嵌入式AI开发笔记》
  • 驾驭时间之舟:SQL中时序数据处理的深度探索
  • ActiveMQ、RabbitMQ、Kafka、RocketMQ在事务性消息、性能、高可用和容错、定时消息、负载均衡、刷盘策略的区别
  • MFC系列-改变控件字体和颜色
  • 【经典算法】BFS_FloodFill算法
  • flume系列之:java.lang.OutOfMemoryError: unable to create new native thread
  • 【前端VUE】npm i 出现版本错误等报错 简单直接解决命令
  • 使用Windows11搭建代理服务器
  • 出海笔记精华问答|第三期
  • 【Leetcode 645 】 错误的集合 —— 纯数学 之 等差数列求和
  • 【大模拟】逻辑回环类
  • QT:QTableWidget 如何不显示行头?
  • FPGA串口调试中当电脑串口无法正常通信,设备管理器中“其它设备”位置显示“USB-Blaster”显示感叹号等问题应该怎么解决?
  • vue3传时间值,还有定义文本域最大值
  • 客户端与服务器通讯详解(7):常见的报错与处置方式
  • E-HPC支持多队列管理和自动伸缩
  • git 常用命令
  • JavaWeb(学习笔记二)
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • ng6--错误信息小结(持续更新)
  • npx命令介绍
  • Tornado学习笔记(1)
  • 当SetTimeout遇到了字符串
  • 分享几个不错的工具
  • 开源地图数据可视化库——mapnik
  • 利用DataURL技术在网页上显示图片
  • 设计模式(12)迭代器模式(讲解+应用)
  • 我的面试准备过程--容器(更新中)
  • postgresql行列转换函数
  • 通过调用文摘列表API获取文摘
  • ​必胜客礼品卡回收多少钱,回收平台哪家好
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • #APPINVENTOR学习记录
  • #进阶:轻量级ORM框架Dapper的使用教程与原理详解
  • #数据结构 笔记一
  • (13)DroneCAN 适配器节点(一)
  • (173)FPGA约束:单周期时序分析或默认时序分析
  • (苍穹外卖)day03菜品管理
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (机器学习的矩阵)(向量、矩阵与多元线性回归)
  • (论文阅读40-45)图像描述1
  • (排序详解之 堆排序)
  • (新)网络工程师考点串讲与真题详解
  • (转)Linux下编译安装log4cxx
  • (总结)Linux下的暴力密码在线破解工具Hydra详解
  • * CIL library *(* CIL module *) : error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodu
  • ./configure,make,make install的作用(转)
  • .NET Core MongoDB数据仓储和工作单元模式封装
  • .net core 使用js,.net core 使用javascript,在.net core项目中怎么使用javascript
  • .NET/C# 阻止屏幕关闭,阻止系统进入睡眠状态
  • .NET应用架构设计:原则、模式与实践 目录预览
  • /etc/skel 目录作用
  • @PostConstruct 注解的方法用于资源的初始化
  • @Tag和@Operation标签失效问题。SpringDoc 2.2.0(OpenApi 3)和Spring Boot 3.1.1集成
  • [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹