How to send signals to processes in Linux and Unix

Killing a process doesn’t always mean stopping it. In this article I will show you how to send signals to processes with kill, pkill and killall and key combinations.

How to kill processes by their PID: kill

The kill command is used to send signals to processes by using the process PID. You can use both, the signal name or the signal number to kill a process:

$ kill -19 1234
$ kill -STOP 1234
$ kill -SIGSTOP 1234

Note: You can kill more than one process with a single kill command: kill -9 1234 4567

To grab the process PID, use pgrep or pidof or ps aux | grep process_name:
I will find out vim’s PID and terminate the vim process:

$ ps aux | grep [v]im
razvan 19180 0.0 0.4 46744 4212 pts/0 T 17:00 0:00 vim
$ kill -9 19180

How to kill processes by name: pkill

The pkill command does the same thing as the kill command, but uses the full or partial name of the process to sent signals. pkill -9 vi and pkill -9 vim will both kill my vim process.

$ ps -ef | grep [v]im
razvan 19388 19080 0 17:22 pts/0 00:00:00 vim
$ pkill -9 vi
[1]+  Killed                  vim

If pkill -9 matches more than one process with that name, it kills all the processes matched.

$ ps -ef | grep [v]im
razvan 19437 19080 4 17:28 pts/0 00:00:00 vim
razvan 19438 19080 5 17:29 pts/0 00:00:00 vim
$ pkill -9 vi
[1]-  Killed                  vim
[2]+  Killed                  vim

How to kill by name all the processes at once: killall

It does not matter how many times I have vim running (one or more), killall -9 vim will kill all my vim processes.

$ killall -9 vim

This killall command is similar to pkill.

The difference between killall -9 process_name and pkill -9 process_name :

killall -9 vi will kill only the processes called vi (will not kill the vim processes) while pkill -9 vi will kill all the processes containing vi in their name (will kill also the vim processes).

How to send signals to processes with key combinations:

  • Ctrl – Z : this sends the SIGSTOP signal to the current process, suspending it. This is mostly used for sending a process in the run in the background.
  • Ctrl – C : this sends the SIGINT signal to the current process, killing it. This is often used when a process freezes.
  • Ctrl – \ : this sends the SIGQUIT signal to the current process. It forces the process to stop.
Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!