Est. reading time: 2 minutes
Linux Basics - the 'kill' command and process signals

Linux Basics – the ‘kill’ command and process signals

The kill command is used in Linux to terminate a running process. Various signals may be passed via the kill command to either terminate the process gracefully, tell the process to restart, or shut it down forcefully. We will first cover the process table and then some commonly used signals.

Process Table

The process table describes all the processes that are currently loaded. The ps command shows the processes. By default, it shows only processes that maintain a connection with a terminal, a console, a serial line, or a pseudo-terminal. Other processes that can run without communication with a user on a terminal are system processes in which Linux manages shared resources. To see all processes, we use -e option and -f to get full information (ps -ef). Below is some sample output from ps -ef.

root         1     0  0  2010 ?        00:01:48 init 
root     21033     1  0 Apr04 ?        00:00:39 crond
root     24765     1  0 Apr08 ?        00:00:01 /usr/sbin/httpd

Note that the init process is always PID (Process Identifier) one. Killing this process is not recommended unless you plan on crashing your system. Below is an image depicting the logic behind the process control and state. The PID for other processes is an integer between 2 and 32,768.

Linux Baics - the kill command

Process Signals

The entries in the “Action” column of the tables below specify the default disposition for each signal, as follows:

  • Term*   Default action is to terminate the process.
  • Ign      Default action is to ignore the signal.
  • Core   Default action is to terminate the process and dump core.
  • Stop   Default action is to stop the process.
  • Cont   Default action is to continue the process if it is currently stopped.

99% of the time we will be using a Terminate switch such as SIGHUP, SIGTERM, or SIGKILL.

The signals SIGKILL and SIGSTOP cannot be caught, blocked or ignored.Linux Signals

Kill command to forcefully kill a process

kill -9 is used to forcefully terminate a process in Linux ( 9 = SIGKILL ). The syntax of the kill command is:

ps -ef| grep process_identifier // will give you PID
kill -9 PID

Killing Multiple Processes

With the kill command, you can specify multiple PID’s at the same time, and all the processes will be signaled as per the example below:

kill -9 pid1 pid 2

You can also script a kill if there are many of the same processes as follows:

for ID in $(ps -ef |grep process_identifier |grep -v grep |awk '{print $2}'); do kill -9 $ID ;  done

---- OR ------

for ID in $(pgrep process_identifier); do kill -9 $ID ; done

Hopefully, these tips will help you on your path to a top Linux Administrator.

Happy Hosting!

Related posts