kill_port
English
Introduction
kill_port is a utility library for developers. It provides a function to find and terminate processes listening on a specified network port. This is particularly useful in development environments where a port might be unexpectedly occupied, preventing an application from starting.
Usage
Add kill_port as a dependency in your Cargo.toml, then call the function as shown below.
The following example demonstrates how to terminate a process listening on port 8080.
use ;
Design Philosophy
The library's design is direct and minimalist, adhering to the principle of doing one thing well.
The process flow is as follows:
- The
kill_portfunction accepts a port number (u16). - It retrieves its own process ID to prevent self-termination.
- It uses the
listeners::get_processes_by_portfunction to identify all processes currently listening on the specified port. - It iterates through the list of found processes.
- For each process that is not the program itself, it uses
nix::sys::signal::killto send aSIGTERMsignal, requesting a graceful shutdown.
This approach ensures that only the target processes are terminated, leaving the calling process unaffected.
Technology Stack
- Language: Rust
- Core Crates:
listeners: For discovering processes associated with a specific network port.nix: Provides safe, low-level bindings to POSIX APIs, used here for sending termination signals to processes.
File Structure
The project is organized with a standard Rust library structure.
.
├── Cargo.toml # Package manifest and dependencies
├── README.mdt # Project documentation template
├── src
│ └── lib.rs # Core library code
└── tests
└── main.rs # Integration tests
A Little Story: The kill Command
The kill command, a staple for developers and system administrators, was introduced in one of the earliest versions of the Unix operating system. Its name is somewhat misleading; the command does not inherently "kill" processes. Instead, it sends signals.
A signal is a form of inter-process communication. When kill is invoked (e.g., kill <PID>), it sends the default SIGTERM (terminate) signal. This is a polite request asking the process to shut down gracefully, allowing it to save its state and release resources. If a process is stubborn, one might resort to kill -9 <PID>, which sends the SIGKILL signal. SIGKILL cannot be ignored by the process, forcing an immediate and often unclean termination. This distinction between "asking" and "forcing" has been a fundamental concept in process management for decades.
中文
项目简介
kill_port 是一个面向开发者的工具库。它提供了一个函数,用于查找并终止监听指定网络端口的进程。在开发环境中,端口时常被意外占用,导致应用程序无法启动,此工具可用于解决该问题。
项目使用
在 Cargo.toml 中添加 kill_port 作为依赖,然后如下所示调用函数。
以下示例演示了如何终止监听 8080 端口的进程。
use ;
设计思路
该库的设计直接且简约,遵循“做好一件事”的原则。
其调用流程如下:
kill_port函数接收一个端口号 (u16) 作为参数。- 获取当前进程ID,以避免自我终止。
- 使用
listeners::get_processes_by_port函数识别所有正在监听指定端口的进程。 - 遍历找到的进程列表。
- 对于每个非自身的进程,使用
nix::sys::signal::kill发送SIGTERM信号,请求其优雅关闭。
这种方法确保只终止目标进程,而不影响调用进程本身。
技术堆栈
- 语言: Rust
- 核心依赖包:
listeners: 用于发现与特定网络端口关联的进程。nix: 提供对 POSIX API 的安全、底层绑定,此处用于向进程发送终止信号。
文件结构
项目采用标准的 Rust 库结构进行组织。
.
├── Cargo.toml # 包清单与依赖项
├── README.mdt # 项目文档模板
├── src
│ └── lib.rs # 核心库代码
└── tests
└── main.rs # 集成测试
相关故事:kill 命令
kill 命令是开发者和系统管理员的必备工具,它诞生于最早期的 Unix 操作系统版本之一。它的名字有些误导性;该命令本身并不直接“杀死”进程,而是发送信号。
信号是一种进程间通信的形式。当调用 kill (例如 kill <PID>) 时,它会发送默认的 SIGTERM (终止) 信号。这是一个礼貌的请求,要求进程优雅地关闭,允许其保存状态并释放资源。如果一个进程“顽固不化”,人们可能会使用 kill -9 <PID>,它会发送 SIGKILL 信号。SIGKILL 信号无法被进程忽略,会强制其立即、且通常是不干净地终止。几十年来,这种“请求”与“强制”之间的区别一直是进程管理中的一个基本概念。
About
This project is an open-source component of i18n.site ⋅ Internationalization Solution.
-
i18 : MarkDown Command Line Translation Tool
The translation perfectly maintains the Markdown format.
It recognizes file changes and only translates the modified files.
The translated Markdown content is editable; if you modify the original text and translate it again, manually edited translations will not be overwritten (as long as the original text has not been changed).
-
i18n.site : MarkDown Multi-language Static Site Generator
Optimized for a better reading experience
关于
本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。
-
翻译能够完美保持 Markdown 的格式。能识别文件的修改,仅翻译有变动的文件。
Markdown 翻译内容可编辑;如果你修改原文并再次机器翻译,手动修改过的翻译不会被覆盖 ( 如果这段原文没有被修改 )。
-
i18n.site : MarkDown 多语言静态站点生成器 为阅读体验而优化。