Kill Process in Linux or Terminate a Process in UNIX / Linux Systems

Linux中终止进程的方法
本文介绍如何在Linux系统中使用kill命令终止进程。包括查找进程ID(PID),发送默认信号和强力信号,以及批量终止多个进程的方法。适用于新手用户。

Iam a new Linux system user. How do I kill process on Linux based server using command line options? How can I kill running process on Unix?

Linux and Unix-like operating system come with thekill command to terminates stalled or unwanted processeswithout having to log out or restart the server.

The kill command sends the specified signal such as kill process to the specified process or process groups. If no signal is specified, the TERM signal is sent. Please note that kill command can be internal as part of modern shells built-in function or external located at /bin/kill. Usage and syntax remain similar regardless internal or external kill command.

Tutorial details
Difficulty Easy(rss)
Root privileges Yes
Requirements kill
Estimated completion time 10 minutes

A list of commonTermsingles

Linux and Unix-like operating system supports the standard terminate signals listed below:

  1. SIGHUP(1) - Hangup detected on controlling terminal or death of controlling process. Use SIGHUP toreload configuration files and open/close logfiles.
  2. SIGKILL(9) - Kill signal. Use SIGKILL as alast resortto kill process. This will not save data or cleaning kill the process.
  3. SIGTERM(15) - Termination signal. This isthe default and safest wayto kill process.

What is a PID?

A Linux or Unix process is running instance of a program. For example, Firefox is a running process if you are browsing the Internet. Each time you start Firefox browser, the system is automatically assigned a unique process identification number (PID). A PID is automatically assigned to each process when it is created on the system. To find out PID of firefox or httpd process use the following command:

pidof httpd
pidof apache2
pidof firefox

OR use the combination ofandgrep command:

ps aux | grep httpd
ps aux | grep apache2
ps aux | grep  firefox

Sample outputs:

Fig.01: Find the process ID (PID) of a running firefox program and apache2 server.

Fig.01: Find the process ID (PID) of a running firefox program and apache2 server.

kill command syntax

The syntax is:

kill [signal] PID
kill -15 PID
kill -9 PID
kill -SIGTERM PID
kill [options] -SIGTERM PID

What Linux or Unix permissions do I need to kill a process?

Rules are simple:

  1. You can kill all your own process.
  2. Only root user can kill system level process.
  3. Only root user can kill process started by other users.

kill command examples

In this example, I am going to kill lighttpd server.

Step #1: Find out the PID (process id)

Use the ps or pidof command to find out PID for any program. For example, if process name is lighttpd, you can use any one of the following command to obtain process ID:

pidof lighttpd

Sample outputs:

3486

OR

ps aux | grep lighttpd

Sample outputs:

lighttpd  3486  0.0  0.1   4248  1432 ?        S    Jul31   0:00 /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
lighttpd  3492  0.0  0.5  13752  3936 ?        Ss   Jul31   0:00 /usr/bin/php5-cg
Step #2: kill the process using a PID

The PID # 3486 is assigned to the lighttpd process. To kill the lighttpd server, you need to pass a PID as follows:
# kill 3486
OR
$ sudo kill 3486
This will terminate a process with a PID of 3486.

How do I verify that the process is gone / killed?

Use the ps or pidof command:
$ ps aux | grep lighttpd
$ pidof lighttpd

A note about sending stronger signal # 9 (SIGKILL)

If no signal specified in the kill command, signal # 15 (SIGTERM), is sent by default. So thekill 3486command is same as the following command:
# kill -15 3486
# kill -SIGTERM 3486

OR
$ sudo kill -15 3486
$ sudo kill -SIGTERM 3486

Sometime signal # 15 is not sufficient. For example, lighttpd may not be killed by signal #15 due to open sockets. In that case process (PID) # 3486 would be killed with the powerful signal # 9:
# kill -9 3486
# kill -SIGKILL 3486

OR
$ sudo kill -9 3486
$ sudo kill -SIGKILL 3486

Where,

  • -9or-SIGKILL- A special kill signal that nearly guarantee to kill the process with the iron fist.
How can I kill two or more PIDs?

The syntax is as follows to kill two or more PIDs as required can be used in a single command:

kill  pid1 pid2 pid3
kill -15  pid1 pid2 pid3
kill -9  pid1 pid2 pid3
kill  -9 3546 5557 4242

Say hello to killall command

This is a Linux only command. to kill processes by name. So no need to find the PIDs using the 'pidof process' or 'ps aux | grep process' command. Do not use killall command on Unix operating systems. This is a Linux specific command.

The syntax is

killall Process-Name-Here

To kill the lighttpd server, enter:
# killall -15 lighttpd
OR
# killall -9 lighttpd
To kill the Firefox web-browser process, enter:
# killall -9 firefox-bin

As I said earlier, the killall command on UNIX-like system does something else. It kills all process and not just specific process. Do not use killall on UNIX system.



http://www.cyberciti.biz/faq/kill-process-in-linux-or-terminate-a-process-in-unix-or-linux-systems/

<think> 我们正在讨论Scrapy框架中的扩展中间件。根据之前的对话,用户已经了解Scrapy有四种核心中间件类型:爬虫中间件、下载器中间件、扩展中间件(Extensions)和项目管道(Item Pipelines)。现在用户特别询问关于自定义扩展中间件的方法和示例。 扩展中间件(Extensions)是通过Scrapy的信号系统(signals)来工作的,它们可以在Scrapy运行的各个阶段插入自定义逻辑。创建自定义扩展需要以下步骤: 1. **定义扩展类**:创建一个类,并实现必要的方法。 2. **连接到信号**:在类中连接到Scrapy的各种信号(例如`spider_opened`, `spider_closed`等)。 3. **注册扩展**:在Scrapy的配置文件中启用扩展。 下面是一个自定义扩展的示例,该扩展在爬虫启动和关闭时打印日志,并在爬虫运行期间定期打印统计信息(模拟定时监控功能): ```python import logging from scrapy import signals from scrapy.exceptions import NotConfigured logger = logging.getLogger(__name__) class MyExtension: """自定义扩展,用于监控爬虫运行状态""" def __init__(self, stats, interval=60.0): # 初始化统计信息和间隔时间 self.stats = stats self.interval = interval # 定时任务句柄 self.task = None @classmethod def from_crawler(cls, crawler): # 获取配置中的间隔时间,如果没有配置则使用默认值60秒 interval = crawler.settings.getfloat('MYEXT_INTERVAL', 60.0) # 如果扩展未启用,则抛出异常 if not crawler.settings.getbool('MYEXT_ENABLED', True): raise NotConfigured # 创建扩展实例 ext = cls(crawler.stats, interval) # 连接信号 crawler.signals.connect(ext.spider_opened, signal=signals.spider_opened) crawler.signals.connect(ext.spider_closed, signal=signals.spider_closed) return ext def spider_opened(self, spider): """爬虫启动时执行""" logger.info(f"Spider opened: {spider.name}") # 启动定时任务 from twisted.internet import task self.task = task.LoopingCall(self.log_stats, spider) self.task.start(self.interval) def log_stats(self, spider): """定时记录统计信息""" stats = self.stats.get_stats() logger.info(f"Spider {spider.name} stats: {stats}") def spider_closed(self, spider): """爬虫关闭时执行""" logger.info(f"Spider closed: {spider.name}") # 停止定时任务 if self.task and self.task.running: self.task.stop() ``` 在项目的`settings.py`中启用该扩展: ```python EXTENSIONS = { 'myproject.extensions.MyExtension': 500, # 优先级设为500 } MYEXT_ENABLED = True # 默认已启用,此项可省略 MYEXT_INTERVAL = 30.0 # 设置间隔时间为30秒 ``` 这个扩展做了以下事情: - 在爬虫启动时开始一个定时任务,每隔一段时间(通过`MYEXT_INTERVAL`设置)打印一次统计信息。 - 在爬虫关闭时停止定时任务。 注意: - 扩展类必须实现`from_crawler`类方法,用于创建扩展实例并连接信号。 - 使用`crawler.signals.connect`来连接信号处理函数。 - 扩展的执行顺序由`EXTENSIONS`设置中的优先级数值决定(数值越小越先执行)。 通过这种方式,我们可以轻松地扩展Scrapy的功能,例如实现监控、报警、资源清理等。 §§相关问题§§ 1. Scrapy扩展中间件如何接入外部监控系统(如Prometheus)? 2. 如何在Scrapy扩展中处理异常并发送警报? 3. Scrapy的信号系统支持哪些关键生命周期事件? </think> ### Scrapy自定义扩展中间件方法与示例 在Scrapy框架中,自定义扩展中间件(Extensions)是通过订阅**事件系统(signals)**实现的全局功能增强组件。它不属于请求/响应流水线,但能访问Scrapy核心对象(如crawler、stats),常用于监控、通知等场景。 #### 创建方法 1. **定义扩展类** 需实现`from_crawler`类方法和事件处理方法: ```python from scrapy import signals class MyExtension: def __init__(self, crawler): self.crawler = crawler @classmethod def from_crawler(cls, crawler): ext = cls(crawler) # 订阅事件 crawler.signals.connect(ext.spider_opened, signal=signals.spider_opened) crawler.signals.connect(ext.spider_closed, signal=signals.spider_closed) return ext def spider_opened(self, spider): print(f"爬虫启动: {spider.name}") def spider_closed(self, spider, reason): print(f"爬虫关闭: {spider.name}, 原因: {reason}") ``` 2. **注册扩展** 在`settings.py`中激活: ```python EXTENSIONS = { 'myproject.extensions.MyExtension': 500, # 数值表示优先级 } ``` #### 实用示例 **定时性能监控扩展** 每60秒记录爬虫状态: ```python from twisted.internet import task class StatsMonitorExtension: def __init__(self, crawler): self.stats = crawler.stats self.interval = 60 # 秒 self.task = None @classmethod def from_crawler(cls, crawler): ext = cls(crawler) crawler.signals.connect(ext.spider_opened, signals.spider_opened) crawler.signals.connect(ext.spider_closed, signals.spider_closed) return ext def spider_opened(self, spider): self.task = task.LoopingCall(self.log_stats, spider) self.task.start(self.interval) def log_stats(self, spider): stats_data = { "pages": self.stats.get_value('response_received_count', 0), "items": self.stats.get_value('item_scraped_count', 0), "errors": self.stats.get_value('log_count/ERROR', 0) } print(f"[监控] {spider.name}状态: {stats_data}") def spider_closed(self, spider, reason): if self.task and self.task.running: self.task.stop() ``` #### 关键特性 1. **事件订阅机制**: - `engine_started`/`engine_stopped`:引擎启停 - `spider_opened`/`spider_closed`:爬虫启停 - `item_scraped`:数据项抓取成功 - `item_dropped`:数据项被丢弃[^3] 2. **核心对象访问**: ```python def from_crawler(cls, crawler): # 获取配置 interval = crawler.settings.getfloat('STATS_INTERVAL', 60) # 访问统计收集器 stats = crawler.stats # 访问爬虫配置 spider_settings = crawler.spider.settings ``` 3. **执行优先级控制** 通过`EXTENSIONS`字典的值调整顺序(值越小优先级越高)[^1] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值