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

本文介绍如何在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/

### 进程管理中的`Process.terminate`与`kill`方法 在编程进程管理中,终止进程的操作可以通过多种方式实现。两种常见的方法分别是 `Process.terminate` 和 `kill` 函数。 #### 使用 `Process.terminate` 对于某些高级抽象库或框架而言,提供了更易于使用的接口来处理子进程生命周期管理的任务之一就是安全地中止它们的工作。例如,在 Python 的 multiprocessing 库里有 terminate 方法可以立即停止由该对象表示的子进程执行[^1]: ```python from multiprocessing import Process def example_function(): while True: pass # Simulate an infinite loop or long-running task if __name__ == '__main__': p = Process(target=example_function) p.start() # Terminate the process after starting it. p.terminate() # It's good practice to join the process afterward, # ensuring that resources are cleaned up properly. p.join() ``` 这种方法简单直观,适用于不需要复杂信号机制的情况;然而需要注意的是调用此函数可能会导致资源泄漏或其他未定义行为因为没有给目标程序足够的时间来进行清理工作。 #### 使用 `kill` 命令及其变体 另一方面,操作系统层面提供了一组更为底层也更加灵活强大的工具——即发送各种类型的 Unix 信号给指定 PID 所对应的正在运行着的应用实例从而改变其状态甚至强制结束掉它。最常用的就是 SIGTERM(15) 号码所代表的那种温和请求对方自行退出的通知形式以及 SIGKILL(9),后者几乎总是能够无条件地摧毁任何抵抗意志并迅速回收占用的一切物理内存页表项等宝贵计算资源[^2]: ```bash # Send a termination signal (SIGTERM) which allows graceful shutdown kill -s TERM <pid> # Forcefully kill the process without allowing cleanup operations kill -s KILL <pid> ``` 值得注意的是,在 POSIX 线程环境中还存在 pthread_cancel 功能用于取消其他线程上的操作,但这属于多线程范畴而非严格意义上的跨进程通信手段。 综上所述,虽然两者都能达到关闭目的的效果但是具体应用场景决定了应该选用哪一种策略:如果只是想要快速而粗暴地干掉某个不受控的服务端口监听者那么毫无疑问直接杀戮是最优解;反之当考虑到优雅降级和平滑过渡等因素时则建议优先尝试协商式的平和途径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值