How to kill a process in Linux
Overview
Hi there fellow Linuxers. Hopefully you’re all well, primed and ready for new learning adventure because this one is going to be one hell of a ride. Every Linux user that comes to age faces this so-called rite of passage. The very moment you start killing processes is very moment that you entered Linux adulthood. I’m joking of course, but it’s not far from the truth. A moment you start carrying out ‘seek and destroy’ missions on processes is a moment that you’re a on path to be an experienced Linux user.
And managing processes on your Linux system a part of that journey, so that’s why in this article we’ll introduce to you to some of the techniques on how to kill a process in Linux. That said, let’s waste time no more and begin our journey.
How to find a process to kill
For this puropose I’ve started few apps on my test VM so we have processes to kill. But in order to kill a process first you have to find it.
Well that’s not what I meant, ofcourse. By ‘finding’ I meant to list processes and notice the one (or a group) you are looking for and when it comes to listing processes you’re not limited to just one option. One well known command for listing processes is ps.
ps
Well, this is kind of vague. Looking at it you`d probably think that’s all the processes you’re running. But that’s actually not the case. Simply by just adding some option (in this case we’ll add -u, option to show list of users) you can see there’s more than just meets the eye.
ps -u
As you can see this is more detailed info. For even more detailed info people usually use ‘-aux’ or ’efjx’ (no, I didn’t forgot to put dash).
ps -aux
ps efjx*
Besides ps, you can also use pgrep command:
pgrep -lu orbywan
(where option l is for listing processes and option u is for specifying user)
Another way to list processes is to use pidof. Well, to be quite precise on this one, you can list processes with pidof only if you know its name.
pidof firefox
And the last but not the least is probably the most used command for listing processes - ==top==
top
Just look at that beauty. Plethora of information, real time monitoring of processes…real eye candy, isn’t it? I’m sure you’ll fall in love with this one and use it on a regular basis. Besides mentioned commands, you also have htop and atop, but in order to use them you’ll have to install them.
Now that we’re done with listing processes it’s time to do what we actually came here for - obliteration! Full scale frontal assault.
How to kill a process in Linux
Now, before we dive deeper into this area, for better understanding how things work it’s necessary to mention that kill command sends signals of termination to a process. Which signal in particular depends on what do you want to do and, hence, which option you’ll use. If you type kill -l
you’ll see a full list of signals of which I’ll mention some of them:
- SIGHUP (1) (signal hang up) - signal used to report that user’s terminal is disconnected
- SIGINT (2) (signal interrupt) - signal sent to a process when user presses interrupt combo
Ctrl + C
- SIGKILL (9) (signal kill) - signal that forces termination of a process
- SIGTERM (15) (signal termination) - somewhat similar to SIGKILL as this also sends signal of termination but this one allows a process to terminate gracefully. This is a default way to kill a process. - SIGSTOP (19) (signal stop) - stops a process but it doesn’t terminate it.
First tool I’m about to mention is probably going to surprise you, because it’s none other than top. Yeah, you read it right. top is quite handy all round tool, I’ve told you you’ll fall in love with it. Enough of my romance rambling about top, I’m sure you’re eager to find out how to kill a process with it. Simple, pressing letter k will add a line just before process listing, asking you to enter ID of process to be killed.
After that you’ll be asked to select type of signal to terminate process (default is 15).
Same thing can be achieved with htop too.
Next command is quite simple and self explainatory - kill. Of course to use it properly you’ll need to know process ID (hence why we searched for processes and their IDs):
kill [pID]
As simple as that. Now that you’ve learned how to kill, Emperor wants you to execute Order 66. Ah just kidding, it was to just to good opportunity to miss.
Now that we’ve learned what types of signals there are it’s easy to conlcude that if you want to kill an unresponsive process you’ll have to add -9 after kill command, as -9 is SIGKILL type of signal:
kill -9 [pID]
Of course, there is a way that you don’t waste time searching for a process ID or you decide to terminate an app (and all of its processes) and just go straight eliminate it by its name. So just like you used pgrep while searching for processes by name, here you’ll use pkill command to kill them:
pkill [process name]
As simple as that. But where pkill really shines is when you add options to it. You can combine -u (to specify a certain user) with either -n (for recently started processes) or -o (for oldest processes).
pkill -n -u orbywan
Superb. It killed Files as it was last started process. And last but not the least is killall command. Its synthax is:
killall brave
If you thought pkill is powerful when combined with options wait until you see this one. I’ll just point some of options that you can use:
- -u (user) - specify which user’s processes you want to kill
- -i (interactive) - you’ll be asked for confirmation of termination
- -v (verbose) - you’ll be reported whether process was succssfully killed or not
- -g (process group) - killing a whole group of processes to which a process belongs to
- -w (wait) - waiting for killed processes to die
killall -i brave
And specially pay attention to these two:
- -o (older-than) - kill processes older than specified time
- -y (younger-than) - kill processes younger than specified time
And when you need to specify time you can use whatever you like: seconds (s), minutes (m), hours (h), days (d), weeks (w), months (M) or years (y).
killall -y 10m brave
Summary
With this we came to an end of this article. Hope you enjoyed it and that you learned something new. To recap - we covered the steps on how find a process we want to kill and how to kill a linux process with variosu different tools. Practice your skills so you’ll end up better than you were yesterday. Until next time, stay sharp.
Thank you for your time..