Table of Contents
As we all know that when any program or application is running then it is called a process. When the process is created a Process Control Block (PCB) a data structure also gets created. A PCB keeps all the information about the process and it is maintained by the operating system. The PCB is identified by a process ID (PID).
As a system admin, we know that Linux is a multitasking OS. Which means multiple processes (tasks) are running at the same time. There are many instances when you have to kill the process reason could be using a high amount of resources making other processes starve, Unresponsive processes etc.
Therefore, in this article, we will look at some of the commands which are used to delete the process but first of all we need information about the process which we want to delete. In the following section first of all we will find the process information about the process.
Finding the process
There are many ways to find information regarding the PID of the process here we will discuss only two ways.
Using “pidof” command
It finds the process ID of a running program and prints the PID on the terminal. For more information use the manpage of the command. Use the following command to get the PID.
pidof <program_name>
For example, you are running gedit editor on your Linux and you want to find the PID of the gedit. See the following output in my environment
foofunc@ubuntu:~$ pidof gedit 22531 foofunc@ubuntu:~$
Using “ps” command
Command explained in the previous section only shows the PID of the process, you can use the ps command to see the PID and other important information about the process. Refer to manpage for more information.
With the ps
command we use grep
to capture the required process. In this section, we will discuss two ps commands with grep in the above-mentioned example. Let’s see the example first.
ps -ef|grep <process_name>
An example of the above command is:
foofunc@ubuntu:~$ ps -ef|grep gedit UID PID PPID C STIME TTY TIME CMD foofunc 22531 1611 0 13:19 ? 00:00:06 /usr/bin/gedit --gapplication-service foofunc 42507 41021 0 21:20 pts/0 00:00:00 grep --color=auto gedit foofunc@ubuntu:~$
ps aux|grep <process_name>
An example of the above command is:
foofunc@ubuntu:~$ ps aux|grep gedit USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND foofunc 22531 0.0 0.3 862636 91196 ? Sl 13:19 0:06 /usr/bin/gedit --gapplication-service foofunc 42521 0.0 0.0 17676 2704 pts/0 S+ 21:21 0:00 grep --color=auto gedit foofunc@ubuntu:~$
Options mentioned in the above ps
command when using the standard syntax are:
- -e: Select all processes
- -f: Do full-format listing
- -F: Same like Extra full format.
- -l : long format
- -y : Do not show flags.
ps -e ps -ef ps -eF ps -ely
To see every process on the system using BSD syntax. The option mentioned in the command are:
- a and x: list all processes when used together with the x option
- u: Display user oriented format.
ps ax ps axu
Killing the process
To kill a process we will use kill
command. It basically sends the signal to a process. Firstly let us list the different signals by running kill-l
command, it list all the signals. Output of the command is:
foofunc@ubuntu:~$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX foofunc@ubuntu:~$
In the above output we can see that signal 9
is for killing so we will use this signal. See the syntax to kill the command below.
sudo kill -9 pid
Example of the above command is:
foofunc@ubuntu:~$ sudo kill -9 22531 [sudo] password for foofunc: foofunc@ubuntu:~$
Other process kill method.
You can use pkill
and killall
commands to kill the process aswell. They both command use the process name as an argument. Below mentioned command are example of kill gedit process.
pkill
command example :
foofunc@ubuntu:~$ sudo pkill -9 gedit foofunc@ubuntu:~$
killall
command example:
foofunc@ubuntu:~$ sudo killall gedit foofunc@ubuntu:~$
Why kill command is better
pkill
and killall
command use argument process name which is easy to use in comparison to kill
. But as we mentioned before that Linux OSes are multiuser and for example, there can be instances when two users are running same application with pkill
and killall
command we kill application for both user which obviously you don’t want. Therefore using kill
command is considered good.
Summary
In this article, we discussed about the process, how to get information regarding the process and how to kill a process. We saw three different ways to kill the process.
In case you face any issues and have any doubt don’t hesitate to comment. I will reply as soon as possible.
Thank you for another informative blog.