4 Advanced Terminal Commands
4 sections, 3 - 4 hours

{ Processes. }

Objectives

By the end of this chapter, you should be able to:

Define what a process is

Examine processes that are running on your machine

Kill a process using the kill command

What is a process?

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

The 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.)

Try typing ps aux in your terminal and see what you get. You should see something similar to this:

Some important columns are USER, PID, and 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.

kill

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 kill command.

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:

ps aux

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:

kill 10011

After killing the process, you should see your terminal window that was running less go back to a normal terminal prompt.

kill vs. kill -9

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.