Wednesday, September 12, 2018

How to Kill a Process in Linux

https://linuxize.com/post/how-to-kill-a-process-in-linux

Have you ever faced the situation where you launched an application and suddenly while you are using the application it becomes unresponsive and unexpectedly crashes. You try to start the application again, but nothing happens because the original application process never truly shut down completely.
Well it has happened to all of us at some point, hasn’t it? The solution is to terminate or kill the application process. But how?
Luckily, there are several utilities in linux that allows us to the kill errant processes.
In this tutorial we will show you how to use kill and killall utilities to terminate a process in Linux. The main difference between these two tools is that killall terminates running processes based on name, while the kill terminates processes based on Process ID number (PID).
Regular users can kill their own processes, but not those that belong to other users, while the root user can kill all processes.
kill and killall can send a specified signal to a specified processes or process groups. When used without a signal both tools will send -15 (-TERM).
The most commonly used signals are:
  • 1 (-HUP): to restart a process.
  • 9 (-KILL): to kill a process.
  • 15 (-TERM): to gracefully stop a process.
Signals can be specified in three different ways:
  • using number (e.g., -1)
  • with the “SIG” prefix (e.g., -SIGHUP)
  • without the “SIG” prefix (e.g., -HUP).
Use the -l option to list all available signals:
kill -l  # or killall -l
Copy
The steps outlined below will work on all Linux distributions.
Advertisement

Killing processes with the kill command

In order to terminate a process with the kill command, first we need to find the process PID. We can do this through several different commands such as top, ps, pidof and pgrep.
Let’s say our Firefox browser has become unresponsive and we need to kill the Firefox process. To find the process PID we can use the pidof command:
pidof firefox
Copy
The command above will print all Firefox processes:
2551 2514 1963 1856 1771
Copy
Once we know the Firefox processes PIDs we can kill all of them with:
kill -9 2551 2514 1963 1856 1771
Copy

Killing processes with the killall command

The killall command terminates all programs that match a specified name.
Using the same scenario as before, we can kill the Firefox process by typing:
killall -9 firefox
Copy
The killall command accepts several options such as specifying processes running as user, using regular expresion and killing processes younger or older than specified time. You can get a list of all options by typing killall (without any arguments).
For example if we want to terminate all processes running as a user sara we would run the following command:
killall -u sara
Copy

Conclusion

In this tutorial, you learned how to stop unresponsive programs using the kill and killall tools.

No comments:

Post a Comment