Operating systems are bed for applications. These applications are executed as processes. Process start, waits and ends. Through their life time their complete given jobs. In this guide we will look how to kill Linux process in different ways.
To kill process we need to get information about process like process id, owner etc. One way to list Linux process is perhaps the most popular way using
ps command like below.
$ ps aux
We provide aux parameters to get all process with required information about each process. In the screenshot we can see only some of process which are sorted by their PID or process id. PID is important because we will kill process generally according to their PID.
Another useful way to list processes interactively is
top command. This command will provide following screen. Process ID’s are depicted as PID in the first column of the process list table.
Filter Process With Name By Using Grep
We can list all process. Now we will filter process according to their names or parameters. In this example we will use popular Linux tool named
grep . We will pipe the result of the command to the grep command to filter for
watch string. This will only print the process those have the term
$ ps aux | grep watch
We filter process named watch with grep.
Now we can kill the process by providing its PID with kill command provided by Linux. Kill command have following simple syntax.
kill [OPTION] PID
As we see option part is optional by provides useful operations if needed.
Now we provide PID of watch process to kill it.
$ kill 16563
After we list watch process we can’t see watch ls process.
Operating system process terminology provides signals which is send to process like a command. Signals can be kill, pause, interrupt etc. kill command by default sends TERM signal to the process. There is alternative signals. We can list these signals provided by operating system like below.
$ kill -l
Kill Process Forcibly
We have sent TERM signal by default with kill command but there is no response or action about kill process. We can make things more brutal by sending SIGKILL or simply KILL.
$ kill -9 17910
$ kill -SIGKILL 17910
$ kill -KILL 17910
As we see there is different ways to express kill options like -9 or -SIGKILL or -KILL. In the kill example we see that our watch ls process is kill with option -9 .
As we know for security reasons Linux provides process ownership. Process ownership establish security and prevent unprivileged users to interact or kill disowned process. Root user or a user which can get root privileges like
sudoer can kill all process in the system.
Kill All Process For Current User
This is harsh way to kill all process we might kill. If we run this command with a regular user it will kill all process related with the user.
$ kill -9 -1
We have kill all process related user and the remote connection is closed naturally.
Kill Multiple Processes
Up to now we have provided only single PID to the
kill command. There is also another usage by providing multiple PID’s to the kill command. This will kill all provided processes. In the example we select two process those PID’s are 1903 and 1948 and than provide these PIDs to the kill command like below. After the kill command we check their existence again but there is no process.
$ kill 1903 1948