What are the differences and how to use the kill, pkill and killall commands
October 30, 2021
How to shutdown a process by process id
Each of the kill, pkill and killall commands are used to stop processes in Linux. In “How to use ps command to monitor Linux processes”, we looked at a number of ways to identify processes, including name, command, user, and terminal. We also looked at ways to identify processes by their dynamic attributes such as CPU and memory usage.
One way or another, we can define the processes that are running. Knowing their process ID, we can (if needed) stop any of these processes with the kill command. If we wanted to kill process 898, we would use this format:
sudo kill 898
Please be aware that in some cases the process will not terminate and will not display any errors or warnings. In fact, this command is a “recommendation”, it “asks” the process to exit. See the article “Why doesn't the kill command kill the process?” for details.
How to disable a process knowing its name
The pkill command allows you to kill processes by name. Make sure you define the correct process! This command will end the top process.
sudo pkill top
How to stop multiple processes by name
If you have multiple copies of a process running, or the process spawned multiple child processes (as Google Chrome can do), how can you shutdown them all? It's just as easy. We are using the killall command.
We have two instances of top running:
ps -e | grep top
We can terminate both of them with this command:
sudo killall top
This time no response means no problem, that is, both of these processes have been stopped.
Before killing the process
Make sure it's the one you want and make sure it doesn't cause any problems. In particular, it is worth checking this with the ps command running with the -H and --forest options to make sure there are no important child processes that you forgot about. See “How to use ps command to monitor Linux processes” for details.
ps -f --forest -C sshd
- Why doesn't the kill command kill the process? (96.4%)
- How to use the top command to monitor Linux processes (78.3%)
- htop command guide: how to view processes in Linux interactively (78.3%)
- How to use ps command to monitor Linux processes (78.3%)
- Do services need to be restarted when updating packages (57.3%)
- Is it safe to remove configuration files left over from removed packages? (SOLVED) (RANDOM - 50%)