How to Kill a Process From the Command Line?

Sometimes, users need to kill the process on a priority basis which halts the system. Considering its needs, the “kill” and “pkill” commands send a signal to a process to terminate in the running system.

This guide will demonstrate various methods to kill a process via the command line. The content of this guide is mentioned below:

Let’s kill the process with process id.

Method 1: Using Process ID to Kill a Process

To kill a process from the command line, you can use the “kill” command followed by the process ID (PID) of the process you want to terminate. Before the killing process, the process id of the running process can be extracted using the “top” command as below:

Let’s kill the “firefox” running process whose id is “2861”. To do so, the user requires the “sudo” privilege by specifying the process id:

$ sudo kill 2861

The output sends a signal to the process with PID “2861” and causes the termination.

Method 2: Using Process Name to Kill a Process

You can also use the “pkill” command to kill a process by its name rather than its PID.

To do so, follow the below script by specifying the process name “firefox”:

$ sudo pkill firefox

This will kill any processes named “firefox“, regardless of their PIDs. If the process is not found, it returns an error.

Method 3: Forcefully Kill a Process

If the process is not responding to the “kill” or “pkill” command, users can try the kill command with the “-9” option that forces the process to terminate. To do so, first, extract the process id using the “top” command:

Now, kill the process forcefully using the “-9” signal having process id “4373”. For this, the “sudo” privilege is required as below:

$ sudo kill -9 4373

This will send the signal to the process, which cannot be ignored or caught by the process.

Method 4: Kill All Processes

To kill all the processes, you can use the kill command with the -9 -1 options. To do so, follow the below script:

$ kill -9 -1

After executing the above command, all the running processes are terminated in the system.

Note: Users must be careful when killing processes, as abrupt termination can cause data loss or corruption.

That is how the process is done via the command line.

Conclusion

From the command line, you can use the “kill” and “pkill” commands to kill the running process by specifying the ID and name, respectively. Additionally, users can forcefully kill the process by utilizing the “-9” option with the “kill” command. This guide has provided all possible ways to kill the process from the command line.