Hi in this article we will talk about a very easy DevOps interview question. How to kill a running process. Earlier we have talked about VPC design, logging system design, monitoring system design, page fault, etc.
kill, pkill and killall
So we can use kill, pkill and killall commands to kill a process in Linux. All of these commands are wrappers over a kill system call. Kill system call takes process ID and signal to kill the process. Let’s take a look at all three of these commands one by one.
Kill is very simple just pass a process ID and a signal [options] to kill any process. This directly makes the kill system call and the process is killed.
Syntax: kill -signal pid
This command takes the process name as an argument to kill the process. Behind the scene, it also calls the kill system call. To get the process ID to pass to the kill system call it will try to traverse and parse the /proc file system to get the processes whose names are matching with the given argument and then it passes their process id to the kill system call.
Syntax: killall -s sginal process_name
pkill can take partial names and patterns in argument to kill a process. It also makes kill system call so need to get the process ID of the process. pkill can take patterns which is very powerful. But as everyone knows, with great power comes great responsibility, it can kill important processes if the pattern passed is wrong.
Syntax: pkill options pattern
If you don’t know about system calls you can learn about them in the below video.
We have not talked about signals in this article, If you want to read more about signals that can be passed to these commands you can look at this video.
In short, all of these commands use the same system call, the difference is in the way they identify the process to kill.
This was a small article on how to kill a process in Linux. If you are preparing for DevOps and SRE Interviews. Don’t forget to give a look at the below playlist
If you like the article please share and subscribe.