Post Thumbnail

kill – kill a process or send signal in Unix

kill command is used for stopping a process in Unix. It’s also capable of sending a specific signal to a process, allowing it to complete with variable levels of gracefulness.

Stop a Unix process with kill

The simplest form of using kill command needs a process ID. You’re also usually specifying a signal ID (specific action of kill command) – most common is signal 9 (SIGKILL).

Let’s start a sleep process for 60 seconds and make it run in background:

{% highlight command %} [greys@redhat8 ~]$ sleep 60 & [1] 26756 {% endhighlight %}

Perfect! 26756 is the process ID, let’s double-check that:

{% highlight command %} [greys@redhat8 ~]$ ps -aef | grep 26756 greys 26756 26344 0 19:27 pts/0 00:00:00 sleep 60 greys 26758 26344 0 19:27 pts/0 00:00:00 grep –color=auto 26756 {% endhighlight %}

Excellent, that’s the one! Let’s kill this process then:

{% highlight command %} [greys@redhat8 ~]$ kill -9 26756 {% endhighlight %}

Press Enter one more time after typing the command, and you should see the confrimation that process is killed:

{% highlight command %} [1]+ Killed sleep 60 {% endhighlight %}

Signals You Can Send with kill Command

There’s quite a few ways to notify a Unix process that you want it to terminate. Although most commonly we’re using kill -9 and kill -15, there are a few more really useful signals that you should know about. We’ll use in place of a numeric process ID that will be specific to your needs.

[Most Useful kill signals in Unix][kill-signals]

  • SIGHUP (kill -1)
  • SIGINT (kill -2)
  • SIGQUIT (kill -3)
  • SIGKILL (kill -9)
  • SIGUSR1 (kill -10)
  • SIGTERM (kill -15)

Read more here: [Most Useful Process Signals for Kill Command][kill-signals].

Killing other users’ processes

Killing other users’ processes is allowed only when you’re running as root. If you attempt killing someone else’s process, you’ll get an error.

Let’s see if we can find some processes belonging to other users, not my username greys.

{% highlight command %} [greys@redhat8 ~]$ ps -aef … root 26799 797 0 19:32 ? 00:00:00 sleep 60 greys 26800 26344 0 19:32 pts/0 00:00:00 ps -aef {% endhighlight %}

Excellent! Process 26799 is running as root, should be good enough for our experiment!

we’re running as my user id, just to be clear. Let’s double-check with id command:

{% highlight command %} [greys@redhat8 ~]$ id uid=1000(greys) gid=1000(greys) groups=1000(greys),10(wheel) {% endhighlight %}

…this means that I wouldn’t be able to kill process 26799 because it’s running under user root:

{% highlight command %} [greys@redhat8 ~]$ kill -9 26799 -bash: kill: (26799) - Operation not permitted {% endhighlight %}

See also