kill vs kill -9

Linux下VS Code进程管理:kill与pkill实战指南 Linux 下的进程管理是开发者绕不开的基本功,尤其当 VS Code 这类基于 Electron 的应用退出后仍残留多个子进程时,盲目操作很容易造成系统卡顿或数据丢失。理解 kill 与 pkill 的区别,是从“会敲命令”走向“真正会管理进程”的关键。kill 并非直接杀死进程,而是通过发送 SIGTERM/SIGKILL 等信号与进程协作;pkill 则允许按进程名或完整命令行匹配目标,能极大提升批量操作的效率。掌握信号机制的原理,能帮助开发者在清理 VS Code 残留进程、解决 code-serv 阅读详情

Reference:

http://bencane.com/2014/04/01/understanding-the-kill-command-and-how-to-terminate-processes-in-linux/

One of my biggest pet peeves as a Linux sysadmin is when I see users, or even other sysadmins using  kill -9  on the first attempt to terminate a process. The reason this bugs me so much is because it shows either a lack of understanding of the  kill  command or just plain laziness. Rather than going on a long rant about why this is bad, I wanted to write an article about the  kill  command and how  signal  works in Linux.

Using the kill command

In Linux and Unix when you want to stop a running process you can use the kill command via the command line interface. The kill command in it's most basic form is pretty simple to work with, if you want to terminate a process you simply need to know the processes id number.

Finding the PID of a running process

To find the process id or PID of a running process we will use the ps command. This command will list running processes and some information about those processes. The ps command has many options and many methods of showing processes; I could dedicate an article just to ps. For this example, I am just going to use the ps command with the -C flag, this flag can be used to lookup a process by the name of the command thats being run.

Syntax:

# ps -C <command>

Example:

    # ps -C nginx
      PID TTY          TIME CMD
      566 ?        00:00:00 nginx
      567 ?        00:00:00 nginx
      568 ?        00:00:06 nginx
      570 ?        00:00:06 nginx
      571 ?        00:00:06 nginx

In the above example I am using the ps command to search for nginx processes. If you look at the output you will see that the PID for each process is listed in the first column. We will use these numbers to kill the nginx processes.

Killing a process with kill

Now that we have found the PID of the process we want to stop, we can use the kill command to terminate the process.

Syntax:

# kill <pid>

Example:

    root@blog:/# ps -C nginx
      PID TTY          TIME CMD
      566 ?        00:00:00 nginx
      567 ?        00:00:00 nginx
      568 ?        00:00:08 nginx
      570 ?        00:00:09 nginx
      571 ?        00:00:08 nginx
    root@blog:/# kill 571
    root@blog:/# ps -C nginx
      PID TTY          TIME CMD
      566 ?        00:00:00 nginx
      567 ?        00:00:00 nginx
      568 ?        00:00:08 nginx
      570 ?        00:00:09 nginx
     8347 ?        00:00:00 nginx

As you can see in the example by running kill with the 571 PID it stopped the nginx process with a process id of 571. Now it is good to note that another nginx process took the place of the process I killed, this is because I killed a worker process for nginx. In order to stop nginx completely I would need to kill the master nginx process.

Using signals with kill

A somewhat common (though if it happens to you a lot, than that may be sign that something is wrong) issue is when you run kill <pid> on a process and the process does not terminate. This can happen for many reasons but what can you do in those scenarios? Well a common response is to use the killcommand with the -9 flag.

Example:

    root@blog:/# ps -C nginx
      PID TTY          TIME CMD
      566 ?        00:00:00 nginx
      567 ?        00:00:00 nginx
      568 ?        00:00:09 nginx
      570 ?        00:00:09 nginx
     8347 ?        00:00:00 nginx
    root@blog:/# kill -9 570
    root@blog:/# ps -C nginx
      PID TTY          TIME CMD
      566 ?        00:00:00 nginx
      567 ?        00:00:00 nginx
      568 ?        00:00:09 nginx
     8347 ?        00:00:00 nginx
     8564 ?        00:00:00 nginx

So why does -9 work? Well when the kill command is run it is actually sending a singal to the process. By default the kill command will send a SIGTERM signal to the specified process.

The SIGTERM signal tells the process that it should perform it's shutdown proceedures to terminate the process cleanly by closing all log files, connections, etc. The below example is a excerpt of a python application, this snippet of code enables the python application to capture the SIGTERM signal and perform the actions in the killhandle function.

Signal handling in Python:

    def killhandle(signum, frame):
      ''' This will close connections cleanly '''
      line = "SIGTERM detected, shutting down"
      syslog.syslog(syslog.LOG_INFO, line)
      rdb_server.close()
      syslog.closelog()
      sys.exit(0)

    signal.signal(signal.SIGTERM, killhandle)

In the above code example the process is able to close both it's database connection and connection to rsyslog cleanly before exiting. In general it is a good idea for applications to close open file handles and external connections during shutdown, however sometimes these processes can either take a long time or due to other issues not happen at all. Leaving the process in a state where it is not correctly running but also not terminated.

When a process is in a limbo state it is reasonable to send the process the SIGKILL signal, which can be invoked by running the kill command with the -9 flag. Unlike SIGTERM the SIGKILL signal cannot be captured by the process and thus it cannot be ignored. The SIGKILL signal is handled outside of the process completely, and is used to stop the process immediately. The problem with using SIGKILL is that it does not allow an application to close its open files or database connections cleanly and over time could cause other issues; therefor it is generally better to reserve the SIGKILL signal as a last resort.

Signal Numbers and Dispositions

Each signal has a numeric Value and an Action associated to it, the numeric values can be used with commands such as kill to define which signal is sent to the process. Each signal also has an "action" or "disposition" associated with it which defines what type of action this signal should invoke.

Signal Actions

While there are several actions for the various signals on a Linux system, I want to highlight the below as they are the most commonly used signals from a process termination perspective.

  • Term - This action is used to signal that the process should terminate
  • Core - This action is used to signal that the process should core dump and then terminate
Common Signals

Below is a list of a few common signals, the numeric value of that signal, the action that is associated with it and how to send that signal to a process. This list, while not complete, should cover general usage of the kill command.

SIGHUP - 1 - Term

  • The SIGHUP signal is commonly used to tell a process to shutdown and restart, this signal can be caught and ignored by a process.

Syntax:

# kill -1 <pid>
# kill -HUP <pid>
# kill -SIGHUP <pid>


SIGINT - 2 - Term

  • The SIGINT signal is commonly used when a user presses ctrl+c on the keyboard.

Syntax:

# kill -2 <pid>
# kill -INT
# kill -SIGINT


SIGQUIT - 3 - Core

  • The SIGQUIT signal is useful for stopping a process and telling it to create a core dump file. The core file can be useful for debugging applications but keep in mind your system needs to be setup to allow the creation of core files.

Syntax:

# kill -3 <pid>
# kill -QUIT <pid>
# kill -SIGQUIT <pid>


SIGKILL - 9 - Term

  • The SIGKILL signal cannot be ignored by a process and the termination is handled outside of the process itself. This signal is useful for when an application has stopped responding or will not terminate after being given the SIGTERM command. This signal should stop more processes however there are exceptions, such as zombie processes.

Syntax:

# kill -9 <pid>
# kill -KILL <pid>
# kill -SIGKILL <pid>


SIGSEGV - 11 - Core

  • The SIGSEGV signal is generally sent to a process by the kernel when the process is misbehaving, it is used when there is an "Invalid memory reference" and you may commonly see a message such as segmentation fault in log files or via strace. You can also technically call this signal with kill as well; however it is mainly useful for creating core dump files, which can also be performed by using the SIGQUIT signal.

Syntax:

# kill -11 <pid>
# kill -SEGV <pid>
# kill -SIGSEGV <pid>


SIGTERM - 15 - Term

  • The SIGTERM signal is the default signal sent when invoking the kill command. This tells the process to shutdown and is generally accepted as the signal to use when shutting down cleanly. Technically this signal can be ignored, however that is considered a bad practice and is generally avoided.

Syntax:

# kill <pid>
# kill -15 <pid>
# kill -TERM <pid>
# kill -SIGTERM <pid>


It is a good idea for any sysadmin to get familiar with how signal works (man 7 signal) and what each signal really means, but if you are looking for the TL;DR version. Don't run kill -9 unless you really have to. If the process isn't stopping right away give it a bit more time, or try to find out if the process is waiting on a child process to finish before running kill -9.


RocketMQ重启会丢消息吗?从存储原理到刷盘策略的可靠性分析 消息中间件的高可用设计始终是分布式系统架构中的核心命题。在RocketMQ这类高性能消息队列的日常运维中,节点重启是最常见的操作之一,但重启是否会导致消息丢失,取决于存储机制与同步策略的深度配合。RocketMQ基于CommitLog顺序写与Page Cache机制实现高吞吐,而刷盘策略则决定了消息写入后何时才算安全落盘;同步复制与主从架构进一步影响故障切换时的数据一致性。理解这些底层原理,才能准确评估重启风险并制定有效的验证方法。在大规模生产环境中,消息可靠性不仅依赖Broker配置,还涉及消费端offs 阅读详情

相关推荐

Linux进程终止方式详解:从kill到kill -9的深度解析

Linux信号机制是操作系统进程间通信与控制的核心技术之一,通过发送特定信号实现进程管理。其中SIGTERM(15)和SIGKILL(9)是最常用的进程终止信号,前者允许进程执行清理操作实现优雅关闭,后者则强制立即终止进程。理解信号处理原理对保障系统稳定性至关重要,特别是在数据库服务、Java应用等需要事务完整性的场景中。通过合理使用kill命令组合、设置shutdown hook以及掌握僵尸进程处理方法,可以有效避免数据损坏和资源泄漏问题。本文通过真实案例演示不同信号对Python进程的影响,并给出生产环

weixin_30225755的博客 256

linux下执行脚本,提示Command not found解决办法

遇上了这个坑,搞了好久没搞定,参考了前辈的文章,终于搞好了,分享如下 原文地址:http://www.cnblogs.com/GordonWorld/archive/2012/03/19/2406225.html Linux下执行.sh脚本错误:bin/sh^M: bad interpreter: No such file or directory       原因是.sh脚本在

xbnlkdbxl的专栏 6万+

Linux kill -9 和 kill -15 的区别

大家对kill -9 肯定非常熟悉,在工作中也经常用到。特别是你去重启tomcat时。可是多半看来,我们对-9的理解只是表面而已。 很少有人(包括我)认真的去了解一下 kill -n 这个n到底是什么东东。自我批评一下。 好吧。我们来好好的认识一下这个熟悉的陌生人。 1)kill 从help中可以清晰的看到 -n 指的是 信号编号,那问题来了,“信号编号”是哪位大神??? 2)kil...

夫君子之行,静以修身,俭以养德,非淡泊无以明志,非宁静无以致远. 4941

shellcommand notfound最经常处理方案

1,先确认这个sh文件是否编码正确 vi test.sh :set ff=unix :wq 运行测试不行, 2然后修改if中括号里面与判断内容都要保持空格。 3判断内容是字符相等用= 数字相等用-eq

wtfsb的博客 600

基于matlab的心电信号预处理

这是前段时间做的一个课程设计,做的比较简单,没有考虑到太细,只是初步地达到了想要的效果。这次设计主要是对心电信号进行预处理,将其信号中包含的一些干扰滤除或者抑制掉。 一、心电信号 (1)心电信号的特性   人体心电信号是非常微弱的生理低频电信号,通常最大的幅值不超过5mV,信号频率在0.05~100Hz之间。心电信号是通过安装在人体皮肤表面的电极来拾取的。由于电极和皮肤组织之间会发生极化现象

i_weimoli的博客 6万+

SpringCloud 2025王炸更新!GraalVM原生镜像启动速度提升90%实战

在当今云原生和Serverless架构盛行的时代,应用的启动速度已成为衡量技术栈竞争力的关键指标。传统SpringCloud应用由于依赖JVM的动态类加载、反射等机制,启动时间通常超过30秒,在Kubernetes滚动更新和函数计算场景下成为显著性能瓶颈。2025年,SpringCloud迎来里程碑式更新,通过深度集成GraalVM原生镜像技术,实现了启动速度提升90%的惊人突破,让SpringCloud应用正式进入"秒级启动"时代。

小筱在线博客 1098

linux 执行sh文件,提示command not found

set ff=unix 或 :set fileformat=unix。fileformat=dos 或 fileformat=unix。:set ff 或 :set fileformat。确保用户对文件有读写及执行权限。利用如下命令查看文件格式。利用如下命令修改文件格式。:wq (存盘退出)

weixin_45526912的博客 1726

华为海思2026届数字IC秋招笔试真题解析与实战技巧(2025最新版)

本文深度解析华为海思2026届数字IC秋招笔试真题,涵盖高频考点如后端设计、低功耗设计、SystemVerilog语法等核心内容。提供时序违例修复、时钟树综合等实战技巧,帮助考生高效备考。特别关注UPF低功耗设计规范等最新考点,助力考生在秋招笔试中脱颖而出。

lg888的博客 1088

ubuntu 执行sh脚本报错command not found

一、问题描述 环境:Ubuntu16.04 运行sh脚本出错,提示 command not found,如下图所示 二、问题解决 出错的原因是,直接运行sh脚本时,Linux系统会去PATH里寻找有没有该sh脚本,而我目前的目录并不在PATH中,因此Linux找不到命令,返回错误提示 command not found。 故执行sh脚本时指定工作目录即可正常执行,如下所示 ./hello.sh 执行结果如图所示 另外,还要注意给sh脚本执行权限,如 chmod +x hello.sh ...

Deng_alone的博客 2万+

从零构建法律案例数据库:Python爬虫实战与法律文本挖掘全攻略

在当今数字化浪潮席卷全球的背景下,法律行业正经历着前所未有的变革。法律案例数据库作为法律大数据的重要组成部分,已经成为法律从业者、研究人员、法学院学生乃至企业法务部门的必备工具。然而,商业法律数据库如北大法宝、威科先行、LexisNexis等往往价格不菲,且数据访问受限。对于个人研究者或小型机构而言,构建一个定制化的法律案例数据库不仅能节省成本,更能根据特定研究需求灵活调整数据结构。

2201_76125261的博客 537

把 SAP Activate 六个阶段看透:从项目落地到 SAP BTP 扩展与 AI 运营的一体化实践

摘要: SAP Activate 方法论常被误解为简单的项目阶段划分,实则是一套将业务目标、标准流程、系统配置和持续运营串联的实施控制体系。其六个阶段(Discover至Run)构成价值递进的实施通道,核心交付集中在Prepare到Deploy阶段。Discover阶段通过DDA评估确立项目方向与范围,Prepare阶段聚焦可执行性准备,Explore阶段采用Fit-to-Standard工作坊进行需求分层,Realize阶段基于前期决策进行配置与扩展开发。该方法论强调标准化优先,差异化需求通过SAP BT

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 2329

Shell脚本报错:command not found

问题 运行shell脚本直接报错:xxx.sh command not found 解决 将脚本文件变为可执行文件 chmod +x xxx.sh 检查脚本内部的变量赋值,是否留了空格(shell脚本的变量时在变量名和等号之间是不允许有空格) 脚本内引用脚本文件的前面是否使用 ./ ...

Robin_Pi的博客 1万+

Qt —— 调用海康C++版SDK获取摄像头每帧图像,通过OpenCv处理每帧图像后显示在Qt窗口(附:源码)

  关注 笔者 - jxd 微信公众号搜索 “码农总动员” 或 微信扫描下方二维码,了解更多你不知道的XX,O(∩_∩)O

5447

MySQL重启不是kill再start:四类安全重启方式详解

MySQL重启本质上是数据库状态机的一次完整变迁,涉及事务一致性、缓冲池刷盘、binlog同步点和InnoDB恢复机制等底层原理。理解shutdown命令与kill信号的本质区别、动态参数与静态参数的生效边界、以及冷重启/热重载/硬恢复/跨平台启动的行为差异,是保障RPO=0和RTO最小化的技术前提。尤其在高可用场景下,滥用kill -9可能导致redo log不一致、主从延迟激增或crash recovery耗时飙升。本文聚焦MySQL服务生命周期管理,系统解析优雅关闭、在线重载、强制恢复及Windows

weixin_29722783的博客 165

【ComfyUI】Wan2.2 Animate 动作迁移重绘视频生成

本文介绍了一个基于ComfyUI的Wan2.2 Animate视频生成工作流,通过动作迁移和视频重绘技术实现高质量视频创作。该系统融合视频帧重建、动作迁移、图像重绘和音频合成等多种AI技术,核心采用Wan2.2 Animate 14B_fp8模型配合专属VAE解码器和LoRA微调模块。工作流程包含输入预处理、模型加载、特征提取、动作采样生成、解码输出和资源清理六个阶段,通过WanVideo系列节点实现从图像/视频输入到最终视频输出的完整处理。系统支持对参考图像进行视觉特征提取和嵌入生成,结合文本/动作嵌入进

Mr数据杨 2444

Python 爬虫实战:Twitter 数据定向抓取(特定话题 + 用户推文爬取与分析)

在社交媒体研究、舆情监测和市场分析等领域,Twitter 数据具有极高的价值。本文将详细介绍如何使用 Python 构建一个完整的 Twitter 数据爬取系统,包括特定话题和用户推文的爬取与分析。我们将涵盖从认证到数据存储、清洗、分析和可视化的全流程。

u014481728的博客 1034

RocketMQ重启丢消息吗?刷盘与主从复制策略详解

在分布式消息队列中,数据可靠性是架构设计的核心命题,而消息丢失往往源于存储层与复制机制的权衡。消息写入内存(PageCache)即返回成功的异步刷盘模式,虽能大幅提升吞吐,却可能因异常断电或强制终止进程而丢失尾部数据。主从异步复制则可能在主节点故障时产生数据空洞,从而影响消息的完整交付。这些原理决定了消息丢失风险与部署配置息息相关,优雅重启、同步刷盘与同步复制能最大程度保障数据不丢。本文将深入剖析RocketMQ的存储架构与重启场景,解析不同配置下的风险差异,并结合真实案例给出安全操作流程,帮助读者系统性理

weixin_33884611的博客 325

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

Linux kill -9 和 kill -15 的区别_linux kill -9 kill -15(1)

从help中可以清晰的看到 -n 指的是 ***信号编号,***那问题来了,“信号编号”是哪位大神???2)kill -l(查看Linux/Unix的信号变量)这么多呀!!今天就单独说一下和3)(系统会发送一个SIGTERM的信号给对应的程序。当程序接收到该signal后,将会发生以下的事情。

2401_84166794的博客 466

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

AcousticSense AI实操手册:ps aux | grep app_gradio.py进程监控与异常恢复流程

本文介绍了如何在星图GPU平台上自动化部署🎵 AcousticSense AI:视觉化音频流派解析工作站镜像,并详细阐述了其核心Web服务的进程监控与异常恢复流程。通过该镜像,用户可以快速搭建一个能够对音频文件进行智能流派分析与可视化解析的AI应用,确保服务稳定运行。

weixin_30978239的博客 33

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Linux Kill 命令详解(Kill -9与Kill -15)

Linux进程管理:Kill命令信号机制详解 本文全面解析Linux kill命令的信号机制,重点对比kill -9(SIGKILL)和kill -15(SIGTERM)的核心区别。SIGTERM(15)是优雅终止信号,允许进程执行清理操作;而SIGKILL(9)是强制终止信号,立即杀死进程但可能导致资源泄漏。文章提供了渐进式终止流程建议:先尝试SIGTERM,必要时逐步升级到SIGINT(2)、SIGHUP(1),最后才使用SIGKILL(9)。同时介绍了获取进程PID的多种方法、常见信号类型及适用场景,

永不止息 2345

Ubuntu进程管理与目录定位实战指南

进程管理是Linux系统运维的核心基础,涉及进程查看、资源监控和异常处理等关键操作。通过/proc虚拟文件系统可以深入理解进程运行机制,其中cwd链接指向工作目录,exe链接指向可执行文件。在Ubuntu等Linux系统中,ps、top和htop等工具链配合kill信号机制,能有效实现进程的精准定位与安全终止。这些技术在Web服务维护(如Nginx)、端口冲突排查等场景尤为重要,特别是结合pgrep和lsof命令可实现高效的问题诊断。掌握/proc目录探查技巧还能解决文件描述符泄漏、磁盘空间异常等典型运维问

weixin_30431445的博客 195

linux中kill和ps命令,linux中几个简单的系统命令(ps,kill,ifconfig,ping,reboot,halt)...

linux中几个简单的系统命令,其他命令接触到了在补充。1.ps命令:(process status),提供对进程的一次性查看。以及执行官ps命令时那个时刻的进程信息格式:ps[参数]-e   此参数显示所有程序-f   显示uid,ppid,c,stime的栏位信息例如 :输入命令 ps -ef 2.kill命令:删除或者杀死执行中的程序或进程。格式:kill[参数][程序]-l   如果不加...

weixin_35172279的博客 317

CPU 100% 高负载进阶排查与定位

本文基于 Rocky Linux 9 实战环境,通过人为制造 CPU 高负载故障,完整演示从vmstat到toppspstreekill的 CPU 故障排查流程。

weixin_65933702的博客 200

Linux---kill及kill -9的用法及如何实现进程的优雅退出

转载自[https://blog.youkuaiyun.com/slvher/article/details/8977338] 如何在shell中终止一个后台进程? 一个后台服务进程如何实现优雅退出? kill与signals 我们这里所说的kill是指作为shell command的那个kill(相对的,Linux系统中还有个叫做kill的system call,man 2 kill可查看其功能及用法),s...

phonycat的博客 3962

Linux命令---kill

Linux kill -9 和 kill -15 的区别 大家对kill -9 肯定非常熟悉,在工作中也经常用到。特别是你去重启tomcat时。可是多半看来,我们对-9的理解只是表面而已。 很少有人(包括我)认真的去了解一下 kill -n 这个n到底是什么东东。自我批评一下。 好吧。我们来好好的认识一下这个熟悉的陌生人。 1)kill 从help中可以清晰的看到 -n 指的是 信号编号,那问题来了,“信号编号”是哪位大神??? 2)kill -l(查看Linux/Unix的信号变量) ...

weixin_43743711的博客 328
上一篇: /dev/tty /dev/ttyS0
下一篇: exit properly shell script
screaming
博客等级 码龄19年 60粉丝 6原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值