Learn Linux 10: Processes




Modern operating systems are usually multitasking, meaning they create the illusion of doing more than one thing at once by rapidly switching from one executing program to another. The kernel manages this through the use of processes. Processes are how Linux organizes the different programs waiting for their turn at the CPU. This chapter will introduce the following commands:

  • ps - Report a snapshot of current processes
  • top - Display tasks
  • jobs - List active jobs
  • bg - Place a job in the background
  • fg - Place a job in the foreground
  • kill - Send a signal to a process
  • kilall - Kill processes by name
  • shutdown - Shut down or reboot the system

How A Process Works

When a system starts up, the kernel initiates a few of its own activities as processes and launches a program called init. init, in turn, runs a series of shell scripts (located in /etc) called init scripts, which start all the system services. Many of these services are implemented as daemon programs, programs that just sit in the background and do their thing without having any user interface. So, even if we are not logged in, the system is at least a little busy performing routine stuff.

The fact that a program can launch other programs is expressed in the process scheme as a parent process producing a child process.

The kernel maintains information about each process to help keep things organized. For example, each process is assigned a number called a process ID (PID). PIDs are assigned in ascending order, with init always getting PID 1. The kernel also keeps track of the memory assigned to each process, as well as the processes’ readiness to resume execution. Like files, processes also have owners and user IDs, effective user IDs, and so on.

Viewing Processes

The most commonly used command to view processes (there are several) is ps. The ps program has a lot of options, but in its simplest form it is used like this:

[user@linux ~]$ ps
  PID TTY           TIME CMD
 3020 pts/1     00:00:00 bash
10582 pts/1     00:00:00 bash

The result in this example lists two processes, process 3020 and process 10582, which are bash and ps, respectively. As we can see, by default, ps doesn’t show us very much, just the processes associated with the current terminal session. To see more, we need to add some options; however, before we do that, let’s look at the other fields produced by ps. TTY is short for “teletype” and refers to the controlling terminal for the process. Unix is showing its age here. The TIME field is the amount of CPU time consumed by the process. As we can see, neither process makes the computer work very hard.

If we add the option x or aux, we can get a bigger picture of what the system is doing.

[user@linux ~]$ ps x
[user@linux ~]$ ps aux

Viewing Processes Dynamically With top Command

While the ps command can reveal a lot about what the machine is doing, it provides only a snapshot of the machine’s state at the moment the ps command is executed. To see a more dynamic view of the machine’s activity, we use the top command.

[user@linux ~]$ top

Controlling Processes

Now that we can see and monitor processes, let’s gain some control over them. For our experiments, we’re going to use a little program called sleep. sleep command is used to delay for a fixed amount of time during the execution of any script.

Interrupting A Process

Let’s observe what happens when we run sleep. First, enter the sleep command and verify that the program is running. Next, press ctrl-C to interrupt the program.

[user@linux ~]$ sleep 1200
[user@linux ~]$

Putting A Process In The Background

Let’s say we wanted to get the shell prompt back without terminating the sleep program. We can do this by placing the program in the background. Think of the terminal as having a foreground (with stuff visible on the surface like the shell prompt) and a background (with stuff hidden behind the surface). To launch a program so that it is immediately placed in the background, we follow the command with an ampersand (&) character.

[user@linux ~]$ sleep 1200 &
[1] 28236
[user@linux ~]$

After entering the command, some numbers get printed. This message is part of a shell feature called job control. With this message, the shell is telling us that we have started job number 1 ([1]) and that it has PID 28236. If we run ps, we can see our process.

The shell’s job control facility also gives us a way to list the jobs that have been launched from our terminal. Using the jobs command, we can see this list.

[user@linux ~]$ jobs
[1]+ Running     sleep 1200

Returning A Process To The Foreground

To return a process to the foreground, use the fg command.

[user@linux ~]$ jobs
[1]+ Running     sleep 1200
[user@linux ~]$ fg %1
sleep 1200

Stopping (Pausing) A Process

Sometimes we’ll want to stop a process without terminating it. This is often done to allow a foreground process to be moved to the background. To stop a foreground process and place it in the background, press ctrl-Z. Let’s try it. At the command prompt, type sleep 1200, press enter, and then press ctrl-Z.

[user@linux ~]$ sleep 1200
[1]+ Stopped     sleep 1200
[user@linux ~]$

Now we can either continue the program’s execution in the foreground, using the fg command, or resume the program’s execution in the background with the bg command.

[user@linux ~]$ bg %1


The kill command is used to “kill” processes. This allows us to terminate programs that need killing (that is, some kind of pausing or termination).

[user@linux ~]$ sleep 1200 &
[1] 28236
[user@linux ~]$ kill 28236
[1]+ Terminated     sleep 1200

The kill command doesn’t exactly “kill” processes; rather, it sends them signals. Signals are one of several ways that the operating system communicates with programs. We have already seen signals in action with the use of ctrl-C and ctrl-Z. When the terminal receives one of these keystrokes, it sends a signal to the program in the foreground. In the case of ctrl-C, a signal called INT (interrupt) is sent; with ctrl-Z, a signal called TSTP (terminal stop) is sent. Programs, in turn, “listen” for signals and may act upon them as they are received. The fact that a program can listen and act upon signals allows a program to do things such as save work in progress when it is sent a termination signal.

Sending Signals To Processes With kill Command

The kill command is used to send signals to programs. In this example we send a HUP signal with kill.

[user@linux ~]$ sleep 1200 &
[1] 28236
[user@linux ~]$ kill -1 28236
[1]+ Terminated     sleep 1200

Here is a list of common signals:

  • 1 - HUP - Hang up. This is a vestige of the good old days when terminals were attached to remote computers with phone lines and modems. The signal is used to indicate to programs that the controlling terminal has “hung up.” The effect of this signal can be demonstrated by closing a terminal session. The foreground program running on the terminal will be sent the signal and will terminate. This signal is also used by many daemon programs to cause a reinitialization. This means that when a daemon is sent this signal, it will restart and reread its configuration file. The Apache web server is an example of a daemon that uses the HUP signal in this way.
  • 2 - INT - Interrupt. This performs the same function as ctrl-C sent from the terminal. It will usually terminate a program.
  • 9 - KILL - Kill. This signal is special. Whereas programs may choose to handle signals sent to them in different ways, including ignoring them all together, the KILL signal is never actually sent to the target program. Rather, the kernel immediately terminates the process. When a process is terminated in this manner, it is given no opportunity to “clean up” after itself or save its work. For this reason, the KILL signal should be used only as a last resort when other termination signals fail.
  • 15 - TERM - Terminate. This is the default signal sent by the kill command. If a program is still “alive” enough to receive signals, it will terminate.
  • 18 - CONT - Continue. This will restore a process after a STOP or TSTP signal. This signal is sent by the bg and fg commands.
  • 19 - STOP - Stop. This signal causes a process to pause without terminating. Like the KILL signal, it is not sent to the target process, and thus it cannot be ignored.
  • 20 - TSTP - Terminal stop. This is the signal sent by the terminal when ctrl-Z is pressed. Unlike the STOP signal, the TSTP signal is received by the program, but the program may choose to ignore it.

Sending Signals To Multiple Processes With killall Command

It’s also possible to send signals to multiple processes matching a specified program or username by using the killall command.

[user@linux ~]$ sleep 1200 &
[1] 28236
[user@linux ~]$ sleep 1200 &
[1] 28237
[user@linux ~]$ killall sleep
[1]- Terminated     sleep 1200
[2]+ Terminated     sleep 1200

Shutting Down The System

The process of shutting down the system involves the orderly termination of all the processes on the system, as well as performing some vital housekeeping chores (such as syncing all of the mounted file systems) before the system powers off. Four commands can perform this function:

  • halt
  • poweroff
  • reboot
  • shutdown

The first three are pretty self-explanatory and are generally used without any command line options.

[user@linux ~]$ sudo reboot

The shutdown command is a bit more interesting. With it, we can specify which of the actions to perform (halt, power down, or reboot) and provide a time delay to the shutdown event. Most often it is used like this to halt the system.

[user@linux ~]$ sudo shutdown -h now

Remember, as with kill, you must have superuser privileges to send signals to processes that do not belong to you.


Most modern systems feature a mechanism for managing multiple processes. Linux provides a rich set of tools for this purpose. Given that Linux is the world’s most deployed server operating system, this makes a lot of sense.