Check out a free preview of the full Complete Intro to Linux and the Command-Line course:
The "Processes" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian defines a process as any sort of command that is currently running, for instance, running bash from the terminal. Interacting with processes is also explained, as well as why they are important.

Get Unlimited Access Now

Transcript from the "Processes" Lesson

[00:00:00]
>> So we've already looked a little bit at processes cuz I showed you the PS AUX, right? And I showed you all the running processes on a computer. Now, we're gonna get a little bit more in depth into that. So just like everything is a file in Linux, everything that's running is a process.

[00:00:18] And this might not be too unfamiliar for you if you've run Windows or Mac before because you have your process manager or your application monitor, I think is what it's called in Mac, right? It's that same idea. And that's what we're doing here every time that we say PS here in Linux.

[00:00:37] We're saying, if you type ps, which I cannot remember what that actually stands for. I think I have it in here, process something or other Anyway, it doesn't matter. So if you run just ps, that's going to look at what just you as Ubuntu is currently running. And right now, I'm not running anything, I'm running just bash, and then the ps itself, like the actual command that I ran to find what processes are running.

[00:01:13] So let's go ahead and run one. There's another command here called sleep and all it does is wait, just waits for that amount of time. So if I say sleep 1, it'll wait for a second and that's done. If I say sleep 2, waits for two seconds and then it's done.

[00:01:28] So if I say sleep 10 and then if I put an ampersand, which means run in the background, so if I put sleep 10 and then I say ps immediately, you'll see that that sleep command comes up, right? That's because I, Ubuntu, spawned the process that's doing sleep and then it's gonna run.

[00:01:48] And if I run this again after 10 seconds, it's gonna say, hey, by the way since you last ran me, this one finished. And now, I'm just back to the two commands. So, That's what that ends up doing. Let's do a sleep 100 in the background, that's too long.

[00:02:15] What happens if I wanna kill, oops, not that one ps. We saw how to do this before. So this is a PID right here, this PID. This stands for process ID as you might imagine and it's how I can refer to processes in flight. If I wanted to kill that, if you remember kill- 9 or kill- SIGKILL 1783 right there.

[00:02:43] Now, if I go back and look at that again, you can see here that the process was killed. So that's why these PIDs will end up being really useful to how you can refer to processes that are currently in flight. Okay, so that's what just ps by itself does.

[00:03:02] Now, you notice that by just muscle memory, I almost always put aux because normally, I don't care what I, the user, am running, I care what everybody's using. So that's what that aux does is it just tells me, here's everything that everyone on the system is running. And you can see here Ubuntu has these ones down here, roots running some stuff, roots running some other stuff, systemd.

[00:03:28] They're all running various different things and you can see here all the various different processes running. So this ends up being usually such a long list that I'll either do ps aux and you can actually see that in the less. And you can scroll through here using less like this or you'll see me often doing the grep stuff that we're talking about earlier.

[00:03:52] So grep ps, right? And then I can find just the thing that I'm looking for there. So that's how I end up usually navigating around this process list. All right, let's talk about background and foreground. So let's say I have a process that's gonna be running a really long time, right?

[00:04:14] Like I'm gonna put in here sleep 1000. But you could imagine this maybe this is like a thing that goes through and processes all of your images, right? That can take a really long time or maybe you're running a machine learning model and it has to run and do all that backward propagation that takes forever.

[00:04:32] So I ran this process sleep 1000. I don't wanna sit here and wait for 1000 seconds for this to finish. So what I can actually do is I can do Ctrl z and what that does is it stops the process and it puts me back into my interactive shell.

[00:04:46] But what you can actually do is I can say jobs, And you can see here that I have this one here. It's been stopped sleep 1000. And I can actually say bg 1, which refers to this one right here. And what this actually does is it resumes the process in the background.

[00:05:05] So if I look here at jobs again, you can see the sleep 1000 is actually running in the background, but I'm still in my interactive shell. So I don't actually have to wait for it. This is really essential for me because I do like a lot of long-running jobs, like npm install, for example, right?

[00:05:22] If you run npm install, that can end up taking several minutes and I sure as hell am either gonna go joust like that other xkcd comic if nmp is installing or my machine code is compiling. Or I can just have it run in the background and not have to wait for it.

[00:05:40] And if you're curious what's happening, I can say jobs again, you can see this is running. And I can say fg, which stands for foreground 1, and this will actually put me back into the process and I'll resume watching the output. And same thing, Ctrl z, jobs 1, or bg 1 rather, and this will resume it in the background.

[00:06:02] So it's how you can move processes to the background or the foreground. Again, that's very helpful to me because I don't always wanna sit there and wait for it. Now, something that I'm not talking about that I know someone on the stream is probably thinking about right now is, why aren't you teaching me tmx or screen?

[00:06:20] So there's two things, there's two other programs out there that I'm not gonna teach you today because I don't end up using them a whole lot. One is called screen, which allows you to have basically multiple shells within one session, right? So I can actually say switch from this one that I'm looking at right now to switch to like basically another window, but it's not a window, it's within the terminal itself.

[00:06:42] And that's what screen will allow to do for you. I sometimes use screen, but usually not very much. But the nice thing about screen is it allows me to run things in the background and switch to another shell or another window basically. I'll let you look it up if you're interested in doing that.

[00:06:57] And then if you wanna get really advanced, there's one called tmux, which allows you to basically split screens and have one running on one side and one running on another. You can split in multiple ways, do all this kind of stuff. Again, I don't end up using it because I used to use it a lot and I had some problems with it that some commands would run slightly different inside of tmux than they would outside of tmux.

[00:07:23] And once I had that problem, I was like, I am never using this again, this was such a weird bug that I lost a day or two that I just quit cold turkey on using tmux. So I'm sure I'm offending someone who might say that their favorite program is probably tmux, but I'm okay with that.

[00:07:38] It's probably the same crowd that's upset that I work for vs code and not on vi. So, Anyway, if you're not gonna use screen and you're not gonna use tmux, then go ahead and use foreground and background. One thing that does bear mentioning, So let's see, jobs, there's a way you can display the pid as well, I think.

[00:08:05] Yeah, jobs- l. So I can see here, this is 1793. I can now just come in here and say kill- 9 1793 if I just wanna kill that job in the background. If I say jobs now, then it'll tell you it's like that was killed. So that's how you would kill it as well.

[00:08:27] This is more than sufficient for my needs. You can learn screen and you can tmux, but jobs works really well. Cool, any questions about that, about processes, or pids, or jobs, or foregrounds, and backgrounds? Cool, so yeah, that was one thing I was gonna mention. If you put that ampersand on there, it automatically sends it to the background.

[00:08:49] So if I say asleep 1000, And I put that ampersand, that just sends it to the background. The one thing to be careful with that is it doesn't necessarily send the output to the background. So if you're gonna do that, you're gonna have to redirect the output, right?

[00:09:08] But we didn't feel like with the, let's kill that. What you would have to do here is like one or just send that all to output.txt and then do that. Otherwise, you're gonna still get the output to your screen despite the fact that your screen is still gonna be interactive.

[00:09:26] So here, I can say jobs- l and I can just kill- 9 1796, I think I can kill 1797 sometimes, 1797. Cool, and those are both killed.