By the end of this chapter, you should be able to:
A process is a program on your computer that is being run. For example, when you have Google's chrome browser open, that is a process running on your machine. An email application, a terminal window, and even the
ls command are also processes when they are being executed on your machine.
The nice thing about processes is that the operating system ensures that all of the memory for one process cannot be accessed by another process. In other words, if one process crashes, it should not have any effect on the rest of the system.
But how can you tell which processes are running at any given time? And how can you stop a process from the terminal?
ps command is a useful tool for seeing which processes are running on your machine. You can execute the command
ps on its own, but more commonly you'll see the command
ps aux being used. The
a indicates that you're interested in all processes, not just process for your current user;
u ensures that the process owner will be displayed; finally, specifying
x makes sure that you'll see a list of all active processes, not just those attached to a terminal.
(Note: you may be wondering why the command is
ps aux, and not
ps -aux. For some history on the command, check out this Stack Overflow question.)
ps aux in your terminal and see what you get. You should see something similar to this:
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND tim 74874 8.3 1.7 3408348 139912 ?? S 3:34PM 6:39.74 /Applications/Google Chrome.app/Contents/Versions/53.0.2785.116/Google Chrome Helper.app/Contents tim 74858 6.2 2.8 3224740 233060 ?? S 3:34PM 8:34.62 /Applications/Google Chrome.app/Contents/MacOS/Google Chrome tim 61431 2.5 0.6 2698840 53728 ?? R 9:46AM 0:59.49 /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
Some important columns are
COMMAND. The USER column is the username of the user who executed the process. The PID column is a number that uniquely identifies the process. This PID will be useful very soon when we learn how to stop a process.
At times you may have a process that for whatever reason is not responsive. In other words, the process continues to execute when you do not want it to. We can stop the process from running by using the
Let's try it out. First, open any file in terminal using the
less command. The process will continue to run as long as you have not pressed
q to quit. In a separate terminal window, type the following:
Now scroll through the list until you find a process that is running that has been started by your username and whose command's path ends with less. Once you've identified the process, take note of the PID.
Next, to kill the process, type
kill and then the PID and hit enter. For example, if your process looks like the following:
tim 10011 0.0 0.0 2434948 900 s000 S+ 5:32PM 0:00.00 less readme.md
Then your PID is
10011. So to kill less, your command would be the following:
After killing the process, you should see your terminal window that was running less go back to a normal terminal prompt.
Sometimes when you're reading tutorials and a process needs to be killed, you'll see that the command
kill -9 is executed, not
kill. So what's the difference between these two?
Whenever you try to kill a process, a signal is sent to that process telling it to terminate. By default that signal is the
TERM signal. However, if a program has crashed or is frozen for some reason, it's possible that it won't pick up on that signal and the process may not terminate. The -9 flag sends a different signal to the process: according to the manual for
kill, -9 represents the
KILL signal, which is a "non-catchable, non-ignorable kill." If killing a process doesn't work, try killing with -9 and see if that gets the job done.
For more on
kill and the different signals you can send, check out this superuser question.
When you're ready, move on to Finding Files and Folders