How to Kill Process in Bash
This article will first discuss the different concepts related to Linux processes. And after this, we will learn the different ways of killing a process.
Before jumping into the kill
command, we must look at some preliminary concepts.
Simple Process vs. Bash Process
An active or running program in the operating system is called a process. For example, when we run an editor or a browser, the operating system creates a process for each.
On the other hand, a simple executable running in the system through Bash is called a Bash process. For example, a new Bash process is created whenever we run any application, like a calculator through a Bash shell.
Process Identifier Number (PID)
A unique number or identifier assigned to every running process is called PID (Process Identifier). The ps
command shows all the running processes and their PIDs.
-
The
ps
command shows all the running process PID. For example: -
The
pidof
command also shows the PID of the running process. We can find thePID
of the running process using the process name.The above figure shows a
sleep
command is executed in the background, and thepidof
command is used to find the PID of that process. -
The
pgrep
command also shows thePID
of the running process. We can grep the PID by process name using thepgrep
command.The above figure shows a
sleep
command is executed in the background, and thepgrep
command is used to find the PID of that process.
Kill a Process in Bash
There are many options to kill a running process in Bash. Following are a few of the options available to use:
Use the Ctrl+C Signal
We can interrupt or kill a running process and send SIGINT
using the Ctrl+C keys. The Ctrl+C sends the interrupt signal, and the running program is killed, or we can say the running program is interrupted.
The above figure shows a sleep 100
command is interrupted using Ctrl+C. When we kill or interrupt a running program using Ctrl+C, the exit code is 130
, which we can check using the echo $?
command.
Use the kill
Command
We can kill a running process using the kill
command. To kill a running process, we need the process’s PID.
Below is the syntax of the kill
command:
kill <pid>
The kill
command uses the PID of any process to kill it. We can find the PID of any process using the ps
, pidof
, or pgrep
commands.
The above figure shows a process with PID 14857
that is killed using the kill
command.
We can also kill any process using a process name instead of its PID. The pkill
or killall
commands can kill any process with a name.
Following is the syntax of the pkill
and killall
commands:
pkill <name>
killall<name>
All the above kill
commands send SIGKILL
to the running process. If any of the commands hang up the running process, we need to specify the signal number or signal.
The above figure shows a process that is killed using the kill -9
command. We can send a kill signal number or kill signal using kill -SIGKILL <PID>
or kill -9 <PID
commands, where 9
is the SIGKILL
number.
We can also use SIGKILL
or 9
with the pkill
or killall
commands.
The above figure shows a sleep process being killed using the pkill -9
command.