Terminating Processes by Process ID
After understanding the concept of Process IDs (PIDs) in Linux, the next step is to learn how to terminate processes using their PIDs. Terminating a process can be useful in various scenarios, such as stopping a misbehaving or unresponsive application, or gracefully shutting down a process before system shutdown.
The kill
Command
The primary command used to terminate processes in Linux is kill
. This command allows you to send various signals to a running process, including the SIGTERM
signal, which requests the process to terminate gracefully.
To terminate a process by its PID using the kill
command, you can use the following syntax:
kill [signal] [PID]
Here, [signal]
is the signal you want to send to the process, and [PID]
is the Process ID of the target process.
For example, to terminate the nginx
process with PID 12345
, you can use the following command:
kill -SIGTERM 12345
This will send the SIGTERM
signal to the nginx
process, requesting it to terminate gracefully.
Forceful Termination with kill -9
In some cases, a process may not respond to the SIGTERM
signal, or you may need to terminate it immediately. In such scenarios, you can use the SIGKILL
signal, which is a forceful termination signal that the process cannot ignore.
To forcefully terminate a process, you can use the following command:
kill -SIGKILL [PID]
For example, to forcefully terminate the nginx
process with PID 12345
, you can use the following command:
kill -SIGKILL 12345
This will immediately terminate the nginx
process, regardless of its state or whether it can handle the SIGTERM
signal.
By understanding how to terminate processes by their PIDs using the kill
command, you can effectively manage and control the running processes on your Linux system.