Learn to kill process in Linux using kill, pkill and killall commands. Kill processes using PID or process name.
Windows users has task manager where they can monitor running processes and choose to ‘End Task‘ to kill off unwanted/hung/less citical processes to save system resources. Same way, in Linux as well you can kill processes and save on your system resource utilization.
In this article we will walk though steps how to kill process in Linux using kill, pkill and killall commands. These three commands used to kill processes in different manner. To proceed with you should know concept of PID i.e. Process ID. It is the numeric value you will be using as argument in kill commands.
What is PID?
PID is Process ID, its a numeric identification of process in kernel process table. Each process in Linux is identified by PID. PID 1 is always init process in Linux whereas new Linux distributions like RHEL7 has systemd as a PID 1 process. It is parent of all processes. If any process dont have parent or if its parent process is terminated abruptly (zombie process), PID 1 process takes over that child process.
Next question is how to find process id in Linux? It can be obtained using below several commands :
root@kerneltalks # ps -A
PID TTY TIME CMD
1 ? 00:00:05 systemd
2 ? 00:00:00 kthreadd
3 ? 00:00:00 ksoftirqd/0
5 ? 00:00:00 kworker/0:0H
7 ? 00:00:00 migration/0
8 ? 00:00:00 rcu_bh
root@kerneltalks # ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.6 128164 6824 ? Ss Aug29 0:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 20
root 2 0.0 0.0 0 0 ? S Aug29 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? S Aug29 0:00 [ksoftirqd/0]
root 5 0.0 0.0 0 0 ? S< Aug29 0:00 [kworker/0:0H]
root@kerneltalks # pidof systemd
With ps -A command you get list of all running processes and their PID in first column of output. Grep out your desired process from output. With ps aux command you can see more information of processes with PID in second column of output. Alternatively, you can use pidof command when you know exact process name to get its only PID.
Now, you are ready with PID of the process to be killed. Lets move on to killing it!
How to kill process in Linux?
There are few limitations you should consider before killing any PID. They are as below –
- You can kill process which is owned by your userid only.
- You can not kill system processes.
- Only root user can kill other user’s processes.
- Only root can kill system using processes.
After fulfilling all above criteria, you can move ahead to kill PID.
Kill process using kill command
Kill command is used to send specific signals to specified PID. Signal numbers and PID you need to supply to command. Signal used are :
- 1 : Hung up
- 9 : Kill
- 15 : Terminate
Normally 9 signal is used (famous kill -9 command) while 15 is used if 9 dosnt work. Hung up signal is rarely used. Kill process using command syntex ‘kill -signal PID‘ like –
root@kerneltalks # kill -9 8274
Kill process using pkill
If you want to use process name instead of PID then you can use pkill command. But remember to use correct process name. Even small typo can lead you to killing off unwanted process. Syntex is simple, just specify process name to command.
root@kerneltalks # pkill myprocess
Kill process using killall
With above two commands : kill and pkill, you are killing only specific process whose PID or name is specified. This leads its child processes to hung or zombie. To avoid this situation, you can kill process along with all its child processes using killall command.
root@kerneltalks # killall myprocess
As a root you can kill any process including system ones on Linux system. As a normal user you can kill processes owned by you only. Process ID i.e. PID can be obtained using command ps or pidof. This PID or process name can be used to kill process using kill, pkill and killall commands.