 and welcome to this tutorial by filmsbychris.com. I'm Chris, Chris with the K. I hope you're having a good day. Today I'm gonna be looking at multiple ways of killing processes. So by their name. Obviously if you have their process ID, you can kill that ID by using the kill command. If you've never been to it, commandlinefood.com is a great place just to see how different people do different things. And there's always multiple ways to do different things. And different things, especially when it comes to coding. I actually have this on my RSS feed and so I'm constantly getting little commands that people have written out. And there's always people can vote them up, vote them down and also come in with alternatives. And I saw this the other day and I wasn't thrilled with either of these options. They work and if they work, they work. But I think that we can have better, simpler solutions. So I'm gonna show you three tools that exist that do this much shorter. But I'm assuming that if they're doing this this way, they are assuming that they don't have these tools. Otherwise they wouldn't be writing out these commands. There's two of them. One down here with a for loop, which is just unneeded. And then there's another one up here with XRs to kill it. But I think it can be shorter because there's a lot of grepting here that doesn't need to be done. If you watch our previous video on grep, you'll see what we're gonna do here in a moment. But first let's go over tools that you probably have on most Linux systems that you can use that either run one command or two commands to kill things. So first thing we need to do is have a process to kill. So let me come over here. So we've seen up here the commands that I saw. Now we're going to get rid of that. And over here I'm gonna type in sleep and I'll just sleep for 10 minutes. Ooh, okay. So what I'm willing to do is kill that process. So there's two ways I can do this. I can find the process ID of that and type the kill and type the process ID. But let's say you have multiple sleep. Now it's kind of the point that you want to kill all, in this case sleep commands that might be running, the kill all command, which I know is not on, it's on most systems. It's not on maybe a lighter weight if you're running on a router or something small like that. Those desktop systems I'm pretty sure it comes pre-installed. Don't wanna do SSH, wanna do sleep. Kill all sleep and it'll kill all sleep processes. If I go back over here, I can actually run two sleep processes together. So they're both running and I can type in kill all sleep and it will kill both those processes. Let's go back over here and run those processes again. Notice you're getting new process IDs each time. Another command, if you don't have to kill all command, a common command is pkill for process kill. And again we'll do sleep and it kills both those processes. Another option would be, if we come over here, run both those processes again, I can do PID of and type in sleep and it'll give me the process IDs. Each process ID of the sleep commands. So again, kill command, which is on every system as far as I know, if you go kill and give it the process ID, boom, it will kill that process. And if I give it the other process ID, it will kill the other process. Let's start those up again. But we wanna have it to where it kills all of them in one swoop. So what we'll do is we'll do the PID of sleep like that, but then we will pipe that into XARGs. XARGs will let you run basically the output of one command and basically break it up and put it into multiple commands. In this case, we'll just say kill. So basically it's passing it both the process IDs, this ID and this ID, and the XARGs is going to run each of those through the kill command. So that would be, I'm pretty sure PID and XARGs are very, very common, more common than these two, which are these two, which are fairly common. Now again, let's say you don't have any of those three commands, PID of, P kill or a kill all, which I'm sure you probably do on your system. But let's say you didn't. Let's look again at this command here. They're doing the PS, AUX to get a list of all processes. Then they're gripping for SSH and then they're using VGREP to un-grep the one that has the grep line, which if you watch our previous video and I'll touch on that in a moment, and then it's doing that for SSHD so that it doesn't kill any SSSD commands because you might want to kill your connections to another server, but you don't want to kill your server. So that's what they're doing there. And then they're cutting that with AUC and then running that into XRGs. Okay, so let's go ahead and look at that command. So I mean, let's look at our version of that. That's gonna be a little bit shorter. So real quick, let's just kill that and split the screen this way just to make it easier to read. We'll clear this up and we'll start up our sleep commands again. Sleep for 10 minutes and sleep for five minutes just so we can see the difference. They're both running. Oops, and there we go. So I can do PSAUX grep sleep. And that will give me my two sleep commands and my grep command, which killing that isn't, that would work. We could actually do that, but you don't want to run, you don't want to get any error outputs because it's trying to kill a grep command that's no longer running by the time it hits it. So what we can do, they, what they did was they grep VGREP, which means reverse and grep. So now you're ungrepping anything that has grep in it. We don't need to do that if you watch my last tutorial. What we can do is just do our regular expression here, which now changes our grep command. So it doesn't have the word sleep in it anymore. So it doesn't find itself. And all we get is our sleep commands. At this point, we can then pipe it into Oc and we can say, get just the second line here, which has the PID, the process ID. So now we have the process IDs. And in my notes, I use TR after this, because I think XARGs, it has to be all on one line. Let's see, if I do XARGs, kill, I think it only killed one of our two processes. Oh no, maybe I killed both of them. So maybe I don't need the extra command I had in there. Yeah, that works. I had an extra command in there to actually put all those on one line, but it seems like I don't need that. So this is, in my opinion, better than this because it's shorter. Their kill command has dash nine, which means kill it, just kill it no matter what. Or if you just use kill, depending on the process and what it's doing, it may not kill it. If you do dash nine, it is gonna kill it dead, regardless of what type of process it is. So it's up to you whether you put that in there or not. It could be dangerous, depending on what you're doing. So, a little bit shorter here. We eliminated these two grips. Now, if you are doing the SSH, you probably do want to do something with the D, but you could probably put the brackets, use the regular expressions around, no, I don't know. Yeah, if you're doing SSH, you might still want this command in there. Although you can use that probably in one grep command. I could probably improve on that a little bit more for the case with the SSH, since you don't wanna kill the service running. But yeah, if you get about that, mine still cuts out an extra grep command there. So I thought I'd bring it up. But we looked at four different ways to kill processes. We can use the kill all command and give it the process name. We can do pkill and the process name. We can do pid of the process name and then pipe that into xargs. Or if you don't have any of those commands, you can use this ps, a, u, x, grep, ock, and xargs. So you're running four different commands. And actually, five for the kill and you're running it more than once. But it's definitely a shorter way of doing it if you don't have those other tools available to you. Anyway, just different ways of doing the same thing. Thanks for watching. Please visit filmsbychrist.com. That's Chris the K. Check out the links in the description. If you enjoyed this, give it a thumbs up. And if you have another way of doing it, comment below. Let us see what you can do. Thanks and have a great day.