Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Over the course of the next few movies I want to take a look at some Unix commands for managing processes. Think back to Chapter 1 where we talked about the difference between the kernel and the shell. In our case the shell is bash. Whenever we run a command inside our shell, a file executes and it communicates with the kernel. And essentially it says to the kernel, "Hey kernel! There are some things I need to accomplish here. Can you help me out?" The kernel sets aside some memory space and starts a process running in it. Then whenever there's output from that process, it returns it back to the shell for us to see. And whenever the process is finally done, the kernel then closes it out and reclaims that memory space so that it can be used by other processes.
That's what the kernel does. it manages the processes for us. Now we can have processes that are really short, like when we run the echo command. That's a really, really short process. It starts, it does its thing, and then it's over. We could also have longer running processes. Let's say that we're going to print five pages to a printer. That'll start a process and that process will just feed information to the printer. After it submits page 1, it'll go to page 2, and so on. And if the printer runs out of paper, the printer will communicate that back to the process, which will then let us know that the printer has run out of paper. That's an example of a long-running process.
We can also have background processes. For example, if we have a database server like MySQL running, it'll just sit and run in the background waiting for connections, waiting for someone to make a request to it which it'll then respond to. So it just sits in the background waiting to do its thing. Now what we want to do now is be able to see those processes. And the easiest way to do that is with the ps command. So ps stands for process status, and it's essentially giving us a snapshot of the processes that are running. Now there are lots of processes running on my machine. By default what ps is showing me are processes that are owned by me, the user kevin, and also a process which have a controlling terminal.
That is, I am in control of them. They're not background processes. If we want to see processes that are owned by other users as well, we can say ps -a. Still I am not seeing background processes but now I see processes that are not owned by me. Notice that the third line there lists the process they I just ran. ps -a. It's a process that's not owned by me. I own the bash process. Bash started the ps -a running, but it's actually a process that's owned by root. And when it's done doing its thing, it returns the results back to me. So it's a bit of a technical point but we don't necessarily own all the processes that get started.
A lot of times process gets started on our behalf by the kernel, which is acting as root. Now there's one big quirk about ps that you need to know about and that is that because ps has been around a long time and it has been modified in different versions of Unix, there's been a real effort to maintain some backwards compatibility here. So in addition to ps -a, we also can use ps a, without the dash. One is the classic option passed into the command that we've been seeing. The other one is using more like an argument that's being passed in. And you'll see that it gives us very similar results, not exactly the same but very similar.
What we're essentially seeing here are two different implementations of ps. One from one version of Unix and one from another. The reason I mention this is because the most classic way to use ps is ps with aux. Those are the options that we pass in and we typically don't put the hyphen in front it. It's a bit of an odd bird because it doesn't match the options that we're normally used to using with the dash. We can actually do a and x together but the problem is u, because u actually has a different meaning between the two versions, so therefore we can't do it that way.
So we're going to do it this way. a means show me all processes regardless of who they're owned by, u says include a column showing me the user that owns the process, and x says show me the background processes too. That's what those three options mean. Let's take a look at it. ps -aux, and there's a list of all the processes that I have running right now. Let's scroll up the top and take look at the header information. You can see that first column is the User, then is the Process ID. Every process gets assigned a unique ID number to help us keep track of it. It shows us the percentage of the CPU that's being used, the percentage of the memory that's being used.
This is the amount of virtual memory that's taking up. This is showing us the Terminal. In most cases it's a question mark. That often means that Mac OS X launched it. If we scroll down to the bottom you'll see that these processes that we've been working with have 000. That's for our current Terminal. If we own up a new window and ran commands there it would be 001. So let's go back to the top again. We have the status. You don't need to worry about those codes. The time that it started, the amount of time that it's been running, and then the command itself, which is a path to the file where this command got started.
So that gives you some information to browse over and you can see what's going on in your Mac. Now if you want to use other options, the man pages can give you a lot more of those information about them. But the most common way that you'll see it use is ps aux, and that should return a list like this. It'll show you a snapshot of what's happening with different processes at this moment in time. Now the problem with that is that it is a snapshot, and things change. things are constantly changing. And it'll be great if we had something where we can actually watch things like CPU and memory usage as they change over time. And to do that, we've got to use the top command which we'll see in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.