How to kill a process

本文围绕Red Hat Enterprise Linux系统展开,主要探讨如何杀死进程。介绍了kill、killall、pkill等命令的使用方法,如通过PID、进程名或部分进程名来发送信号杀死进程。同时提醒避免常用kill -9,因其无法让程序清理临时文件等,还提及诊断时若进程为'Z'状态则无法杀死。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://access.redhat.com/solutions/71813

 SOLUTION 已验证 - 已更新 2014年四月8日20:06 - 

English 

环境

  • Red Hat Enterprise Linux (All Versions)

问题

  • How to kill a process ?

决议

Kill command is use to send signal to a process or to kill a process. We typically use kill -SIGNAL

There are other ways to effectively kill a process — killing a process by name, killing a process by specifying part of the name, killing a process by pointing out the process with cursor etc.,

  1. Kill Command – Kill the process by specifying its PID

    All the below kill conventions will send the TERM signal to the specified process. For the signals, either the signal name or signal number can be used. You need to lookup the pid for the process and give it as an argument to kill.

    Raw

    $ kill -TERM pid
    
    $ kill -SIGTERM pid
    
    $ kill -15 pid
    

    Example: Kill the firefox process.

    Raw

    $ ps -ef | grep firefox
    1986 ?        Sl     7:22 /usr/lib/firefox-3.5.3/firefox
    
    $ kill -9 1986
    

    Note: It is not a good idea to use kill -9 always, Most programs require some sort of cleanup when it exits. These programs setup a signal handler to perform these cleanup duties as a result of SIGTERM and SIGINT. They would setup a signal handler for SIGKILL if they could, but SIGKILL (kill -9) is untrappable. Using SIGKILL dont give opportunity to cleanup while exiting and dont give opportunity to delete any temporary files, shutdown sockets, remove shared memory segments, close open files, or some other task. This may corrupt database or other important temporary files.

    A process should be sent a SIGTERM signal.

    Raw

    kill 5987
    kill -s TERM 5987
    kill -TERM 5987
    kill -15 5987
    
  2. Killall Command – Kill processes by name

    Instead of specifying a process by its PID, you can specify the name of the process. If more than one process runs with that name, all of them will be killed.

    Example: Kill all the firefox processes

    Raw

    $ killall -9 firefox
    
  3. Pkill Command – Send signal to the process based on its name

    You can send signal to any process by specifying the full name or partial name. So there is no need for you to find out the PID of the process to send the signal.

    Example: Send SIGTERM to all the process which has sample in its name.

    Raw

    $ pkill sample
    

Pkill Example:

Before sending signal, you can verify which are all the process is matching the criteria using “pgrep -l”, which displays the process ID and process name of the matching processes.

In this example, all the processes are designed to log the signal to signal-log, along with its PID.

Raw

$ pgrep -l sample
12406 sample-server.p
12425 sample-server.p
12430 sample-garbagec

$ pkill -USR1 sample

$ cat signal-log
Name: ./sample-server.pl Pid: 12406 Signal Received: USR1
Name: ./sample-server.pl Pid: 12425 Signal Received: USR1
Name: ./sample-garbagecollector.pl Pid: 12430 Signal Received: USR1

Note: The part of name which you specify should be in the character within the first 15 character of the process name.

诊断步骤

  • Check process status. If the process is 'Z' status, you cannot kill it.

Raw

# ps aux|grep -i PROCESS NAME

Raw

To check if signal is block for a specific process run below command

cat /proc/<pid>/status |grep ^Sig
SigQ:   1/62809
SigPnd: 0000000000000000
SigBlk: 0000000056727a01
SigIgn: 0000000000381000
SigCgt: 00000001c98044ff

From above output it is confirmed signals no (1,10,12,13,14,15 etc) are blocked for this process.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值