`

linux_A Guide to Kill, Pkill and Killall Commands to Terminate a Process in Linu

 
阅读更多

via: http://www.tecmint.com/how-to-kill-a-process-in-linux/

 

Linux Operating System comes with Kill command to terminate a process. The command makes it possible to continue running the server without the need of reboot after a major change/update. Here comes the great power of Linux and this is one of the reasons, why Linux is running on 90% of servers, on the planet.

 

Kill command send a signal, a specified signal to be more perfect to a process. The kill command can be executed in a number of ways, directly or from a shell script.

Using kill command from /usr/bin provide you some extra feature to kill a process by process name using pkill. The common syntax for kill command is:

# kill [signal or option] PID(s)

For a kill command a Signal Name could be:

Signal Name		Signal Value			Behaviour

SIGHUP			      1				Hangup
SIGKILL			      9				Kill Signal
SIGTERM			      15			Terminate

Clearly from the behaviour above SIGTERM is the default and safest way to kill a process.SIGHUP is less secure way of killing a process as SIGTERMSIGKILL is the most unsafe way among the above three, to kill a process which terminates a process without saving.

In order to kill a process, we need to know the Process ID of a process. A Process is an instance of a program. Every-time a program starts, automatically an unique PID is generated for that process. Every Process in Linux, have a pid. The first process that starts when Linux System is booted is – init process, hence it is assigned a value of ‘1‘ in most of the cases.

Init is the master process and can not be killed this way, which insures that the master process don’t gets killed accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.

To know all the processes and correspondingly their assigned pid, run.

# ps -A
Sample Output
PID TTY          TIME CMD
    1 ?        00:00:01 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 migration/0
    4 ?        00:00:00 ksoftirqd/0
    5 ?        00:00:00 migration/0
    6 ?        00:00:00 watchdog/0
    7 ?        00:00:01 events/0
    8 ?        00:00:00 cgroup
    9 ?        00:00:00 khelper
   10 ?        00:00:00 netns
   11 ?        00:00:00 async/mgr
   12 ?        00:00:00 pm
   13 ?        00:00:00 sync_supers
   14 ?        00:00:00 bdi-default
   15 ?        00:00:00 kintegrityd/0
   16 ?        00:00:00 kblockd/0
   17 ?        00:00:00 kacpid
   18 ?        00:00:00 kacpi_notify
   19 ?        00:00:00 kacpi_hotplug
   20 ?        00:00:00 ata/0
   21 ?        00:00:00 ata_aux
   22 ?        00:00:00 ksuspend_usbd

How about Customising the above output using syntax as ‘pidof process‘.

# pidof mysqld
Sample Output
1684

Another way to achieve the above goal is to follow the below syntax.

# ps aux | grep mysqld
Sample Output
root      1582  0.0  0.0   5116  1408 ?        S    09:49   0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql     1684  0.1  0.5 136884 21844 ?        Sl   09:49   1:09 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root     20844  0.0  0.0   4356   740 pts/0    S+   21:39   0:00 grep mysqld

Before we step ahead and execute a kill command, some important points to be noted:

  1. A user can kill all his process.
  2. A user can not kill another user’s process.
  3. A user can not kill processes System is using.
  4. A root user can kill System-level-process and the process of any user.

Another way to perform the same function is to execute ‘pgrep‘ command.

# pgrep mysq
Sample Output
3139

To kill the above process PID, use the kill command as shown.

kill -9 3139

The above command will kill the process having pid=3139, where PID is a Numerical Value of process.

Another way to perform the same function, can be rewritten as.

# kill -SIGTERM 3139

Similarly ‘kill -9 PID‘ is similar to ‘kill -SIGKILL PID‘ and vice-versa.

How about killing a process using process name

You must be aware of process name, before killing and entering a wrong process name may screw you.

# pkill mysqld

Kill more than one process at a time.

# kill PID1 PID2 PID3

or

# kill -9 PID1 PID2 PID3

or

# kill -SIGKILL PID1 PID2 PID3

What if a process have too many instances and a number of child processes, we have a command ‘killall‘. This is the only command of this family, which takes process name as argument in-place of process number.

Syntax:
# killall [signal or option] Process Name

To kill all mysql instances along with child processes, use the command as follow.

# killall mysqld

You can always verify the status of the process if it is running or not, using any of the below command.

# service mysql status
# pgrep mysql
# ps -aux | grep mysql

That’s all for now, from my side. I will soon be here again with another Interesting and Informative topic. Till Then, stay tuned, connected to Tecmint and healthy. Don’t forget to give your valuable feedback in comment section.

分享到:
评论

相关推荐

    linuxkill用法、killall、pkill、xkill.pdf

    Linux进程管理工具kill、killall、pkill和xkill的使用 一、 kill 命令的使用 kill 命令是 Linux 系统中最基本的进程管理工具,用于终止进程或程序。kill 命令的使用格式为:kill [信号代码] 进程ID,其中信号代码...

    kill_program_by_name.zip_terminate process

    当我们谈论"terminate a process by name"时,我们的目标是找到并停止与特定名称关联的进程。 在Windows系统中,可以使用`taskkill`命令行工具来完成这个任务。例如,如果你想结束名为"notepad.exe"的进程,可以在...

    linux_kill用法.pdf

    ### Linux Kill 命令详解 #### 一、概述 Linux系统中,有时我们需要终止...通过学习和理解`kill`、`killall`、`pkill`及`xkill`命令,我们可以更有效地管理和控制Linux系统中的进程,从而提高系统的稳定性和安全性。

    user_kill.rar_Linux kill_ROOT

    在Linux和Unix操作系统中,`kill`命令是一个用于发送信号到指定进程的工具,通常用于终止或控制进程的运行。然而,非root用户通常没有权限杀死其他用户的进程,除非他们有特定的权限设置或者进程所有者赋予了这种...

    Linux_CHUXUEZHE.rar_UNIX_linux_linux 教程_linux教程

    在Linux中,每个运行的程序都是一个进程,通过`ps`命令查看当前进程,`kill`或`pkill`发送信号结束进程,`top`实时监控系统资源占用情况。 六、网络配置与通信 Linux提供了强大的网络功能,`ifconfig`配置网络接口...

    Linux_commond.rar_linux_linux commond_tcl

    35. **kill** 和 **pkill**:发送信号给进程,用于终止或控制进程。 36. **top** 和 **htop**:实时监控系统资源使用情况。 37. **ps**:显示当前系统中的进程状态。 38. **netstat**:查看网络连接状态和路由信息。...

    linux_learn_notes_linux_学习笔记_

    - `kill`/`pkill`:发送信号结束进程,如`kill -9 pid`强制结束进程。 - `nohup`:让命令在后台持续运行,即使退出终端也不受影响。 3. 系统信息与网络: - `uname`:显示系统信息,如`uname -a`。 - `ifconfig...

    快乐的linux命令行_linux_

    - `kill` 和 `pkill`: 结束进程。 - `nohup`: 让程序在后台运行,即使用户退出也会继续执行。 9. **网络和系统信息**: - `ifconfig`: 查看和配置网络接口。 - `ping`: 检查网络连接。 - `nslookup` 或 `dig`: ...

    LINUX_changjianwenti.zip_linux

    9. **进程管理**:`ps`、`top`和`htop`等命令用于查看和管理进程,`kill`和`pkill`用于终止进程。 10. **Shell脚本编程**:了解Bash shell脚本编程可以帮助自动化任务,如创建备份脚本、系统维护脚本等。 11. **...

    Linux_order.rar_linux

    系统管理和进程控制方面,`ps`显示当前运行的进程,`top`提供实时的系统资源监控,`kill`和`pkill`用来终止进程,`nohup`让程序在后台运行不受终端关闭影响。`cron`是Linux的定时任务服务,可以设置定期执行的任务。...

    linux_html.7z

    "ps"命令用于查看当前运行的进程,"kill"或"pkill"用于终止进程,"nohup"或"&"符号用于在后台运行程序,"top"或"htop"则提供实时的系统资源监控。 网络和网络服务也是Linux学习的重要部分。"ifconfig"用于配置网络...

    Linux进程管理命令详解:ps、kill、pkill的使用方法

    内容概要:本文详细讲解了在 Linux环境中常用的几个用于管理进程的命令,其中包括如何使用ps查看进程,kill终止指定 PID的进程,以及 pkill和 pgrep通过名字来定位并操作相关进程。此外还介绍了 htop,pstree等可视...

    10 个Shell命令解析器的功能.zip_Process_analysis_linux_report_shell

    `kill`和`pkill`则可以发送信号来管理进程。 6. **环境变量**:Shell使用环境变量来存储系统和用户设置,如PATH变量指示命令搜索路径,HOME指代用户的主目录。用户可以通过`export`命令修改或设置环境变量。 7. **...

    linux-bootcamp_triedeus_linux_boot_

    4. **进程管理**:在Linux中,可以使用`ps`、`top`或`htop`命令查看当前运行的进程,使用`kill`或`pkill`终止进程,`nohup`命令可以让程序在后台持续运行,即使用户注销也不会受到影响。 5. **软件包管理**:不同的...

    linux_linux个人学习笔记_

    你可以使用ps命令查看当前运行的进程,用top或htop命令实时监控系统资源占用情况,而kill和pkill命令则用于终止进程。理解进程的优先级(nice值)和调度策略(实时与非实时)也是进程管理的关键。 四、网络配置 ...

    linux_命令网站

    4. **进程和系统管理**:`ps`显示当前系统中的进程,`top`或`htop`实时监控系统资源使用情况,`kill`和`pkill`用于结束进程,`systemctl`控制服务状态,`uname`显示系统信息。 5. **网络操作**:`ping`测试网络连接...

    2_linux_TheProcess_

    Linux提供了多种工具来管理和查看进程,如`ps`命令用于列出当前系统中的进程信息,`top`和`htop`提供实时的进程监控,`kill`和`pkill`用于发送信号来终止进程。进程间通信(IPC,Inter-Process Communication)是...

    LAB05-CC_linux_

    4. **系统信息和进程管理**:`whoami`显示当前用户名,`ps`显示当前进程,`top`或`htop`实时监控系统资源,`kill`和`pkill`发送信号给进程,`nice`和`renice`调整进程优先级。 5. **软件安装和包管理**:`apt`或`...

Global site tag (gtag.js) - Google Analytics