From the course: Complete Guide to Linux Security: Protecting Your Linux Server Environment

Ending processes with commands

- Let's continue with our lab 31, and we'll show how to end processes with commands in the Linux terminal. We'll go back to our Debbie in client here. Now, there are a variety of ways to terminate processes. You can use commands, you can use the top program, you can use keyboard shortcuts, and the list goes on. But let's show a couple of the commands that you can use. The first one is the kill command. Quite gruesome, but in computer technology, kill is the equivalent of terminate. And which would you rather type, right? Kill is just four characters. So let's show this. Let's go ahead and open the calculator program, and you can find that within your programs in the GUI, or if you want, you can just do that in a second terminal and type gnome-calculator. There it is, built-in calculator in Linux. Works pretty well. That doesn't fork off the terminal, so we need to leave that terminal open. We'll go back to our original terminal. And so let's take a look and view that process first. Building on the knowledge from the last sub-lesson, we'll run a PS aux and pipe that with a grep on calc. There we go. So we have the gnome calculator right here. And you can see that it is process ID 7272. I'm not worried about this. That's just from the command that I just issued. So we locate the process ID for the actual gnome calculator process, and we can end that by using the kill command and that process ID number. Before we do so, let's bring that calculator up and let's watch it go bye-bye. Press enter, and that terminates that calculator program because we terminated the underlying process, right? So this is a fantastic way to terminate non-responsive programs that could be causing system performance degradation issues. You know, if you have that CPU percentage spiking or if your system is sluggish or it's freezing up, could often be because of a specific application. So that's a great way to terminate those. Another way is to use the pkill command, also built into Linux, and you can use that in conjunction with pgrep, which we showed in the previous sub-lesson. So for this one, we're going to make sure that Firefox is open, and it is down here, so that's good. And we'll go ahead and run a ps -aux, and we'll grep on Firefox. And we get all that information. Well, we said, hey, you can also do a ps -e and there's other ways to do it, but let's try it the way we're mentioning where we want to go with that first process ID, which is the same. It's still running from this previous sub-lesson. It's 6232. Now we could kill that process or we could just go by name, firefox-esr. But you can make it even simpler. With the pkill command, you could just use pkill "firefox". And that should kill off the Firefox program altogether. And if I hit the super user key, we can see it is not running anymore and we'll kill that with the X. Now if we run the ps aux on Firefox, again, we're just going to see that we're doing a grep on Firefox currently, and that's it. So that will terminate all instances of Firefox. And you could also type pgrep "firefox" to prove that as well. Now you might remember using this command during the SSH portion of this course. I rely on it heavily in that respect. One more command you can use, and that is the killall commands, but that may or may not be installed on your system. If you don't have it, you can do a sudo apt install psmisc, and you should grab that command and you can see all the options for that here. And this can be used to terminate all processes based on one command name. So let's say we had multiple applications open within an application group. For example, we could open up a calculator and we could just search and open that, and then we could open up a second one, right click, and do new window. Now we have two calculators open and if we wanted to terminate both of those at the same time, we could use that killall command and say gnome-calculator, and that gets rid of all of those calculator programs at the same time. That may work with the pkill command as well in double quotes, but killall can be a pretty helpful command. You can see there's a lot of really good options in here. And there you go. There are some commands that you can use to terminate processes and their programs within Linux.

Contents