Operating systems are a bed for applications. These applications are executed as processes. Process start, waits, and ends. Through their lifetime their complete given jobs. In this guide, we will look at how to kill the Linux process in different ways.
List Running Processes
To kill the process we need to get information about the process like process id, owner, etc. One way to list the Linux process is perhaps the most popular way using
ps command like below.
$ ps aux
We provide aux parameters to get all processes with the required information about each process. In the screenshot, we can see only some of the process which is sorted by their PID or process id. PID is important because we will kill processes generally according to their PID. Another useful way to list processes interactively is
top command. This command will provide the 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 the process. Now we will filter process according to their names or parameters. In this example, we will use a 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 the 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 the PID of the watch process to kill it.
$ kill 16563
After we list the watch process we can’t see watch ls process.
Send Signal To The Process
Operating system process terminology provides signals which are sent to process like a command. Signals can be killed, pause, interrupt, etc. kill command by default sends a TERM signal to the process. There are alternative signals. We can list these signals provided by the operating system like below.
$ kill -l
Kill Process Forcibly
We have sent the TERM signal by default with kill command but there is no response or action about the killing 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 are different ways to express kill options like -9 or -SIGKILL or -KILL. In the kill example, we see that our watch ls process is killed with option -9.
As we know for security reasons Linux provides process ownership. Process ownership establishes security and prevents unprivileged users to interact or kill the disowned process. Root user or a user which can get root privileges like
sudoer can kill all processes in the system.
Kill All Processes For The Current User
This is a harsh way to kill all the processes we might kill. If we run this command with a regular user it will kill all processes related to the user.
$ kill -9 -1
We have killed all processes related to the 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 PIDs to the kill command. This will kill all the provided processes. In the example, we select two processes those PIDs are 1903 and 1948 and then 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