linux kill process

Kill a Process

Linux Tips by Burleson Consulting

When good processes go bad it is often necessary to terminate them.  The kill command is used to send a termination signal to the specified process or group. The default termination signal is a SIGTERM (15) signal.  A process which receives a SIGTERM may be programmed to trap signals and perform specific functions or ignore the signal entirely.

kill takes a process ID as a parameter.  The process ID can be found using any of the methods described earlier in this chapter.

The following example shows the xscreensaver process (2609) which is to be terminated.

$ pgrep xscreen
2609
$ kill 2609

Though we use pgrep to find the PID of this process here we could have also used a PID from the output of top or ps.

When there a stubborn process trapping the kill command and refusing to terminate, the -9 (SIGKILL) signal should be used to kill the process. The -9 signal cannot be trapped by a process and ignored. If the xscreensaver process did not terminate after the last kill command, the new kill command would look like the following:

$ kill -9 2609

While the kill command is typically availble to all users you will only be able to kill processes you own.  To kill processes owned by other users you will need root privileges.

Kill Processes Using a Pattern

Rather than scanning through process ID (PID) numbers, it might be easier to kill a process or group of processes by searching for a pattern in the command name field.  The pkill command is one way to accomplish this, as shown below.  pkill matches processes in the same way pgrep does.  Be very careful when using pkill as you could easily kill more than you wanted to!

Here we want to kill all processes called xscreen.  First we use pgrep to make sure we get a reasonable number of PIDs back, then we kill the process with pkill.

$ pgrep xscreen
2609
$ pkill xscreen

Again, be careful using pkill, especially as root!

Kill All Processes Owned By a Particular User

The pkill command also recognizes the –u option to look only at a specific user's processes. By adding the -u option to the pkill command, it is possible to terminate any and all processes for the specified user. If, for example, the following command was entered by user terry, all terry’s active processes would be cancelled, and terry would be kicked off the system.

$ pkill -9 –u terry

* Please save any work in progress before trying this!

Since you have the ability to kill any user's processes this would be a quick way to stop everything they're doing.  Be careful though, doing so may not make you any friends, if you know what I mean.

We've talked mostly about two users so far, root and you (terry in most of the examples,) but there could be more people than that on our Linux system!  Next we'll look at how to tell who's logged in.

 


This is an excerpt from "Easy Linux Commands" by Linux guru Jon Emmons.  You can purchase it for only $19.95 (30%-off) at this link.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值