Introduction

This tutorial talks about how to list the running processes or running programs and commands. The difference between foreground and background processes, how to manipulate those are also introduced. This tutorial also talks about how to kill processes.

Listing Processes and Information

To display the currently running processes use the ps command. If no options are specified, ps displays the processes associated with your current session.

Command Meaning
ps Display the processes associated with your current session.

Process Options

To see every process including others that are not owned or run by you, use -e option. The -f option will give you more information and a full format listing. To see processes running as a specified user, use ps -u username . And to display more information about a specific process, use -p option and followed by it's PID (Process Idenfification number), e.g. ps -p pid.

Options Meaning
-e Display every processes including others not owned by you.
-f Full format listing.
-u username Display username's processes.
-p pid Display information for PID.

Common ps commands

Here are some common ps commands that you could use.

Commands Meaning
ps -e Display all processes.
ps -ef Display all processes with full format.
ps -eH Display a process tree.
ps -e --forest Display a process tree.
ps -u username Display user's processes.

There are other ways to view processes and running commands on a system. You can use pstree command, it displays processes in a tree format. You can use top or htop to display the interactive processes view. htop is less common than top , so it may not be included in your Linux distribution.

Commands Meaning
pstree Display processes in a tree format
top Interactive process viewer
htop Interactive process viewer

Examples

To see a list of processes that are associated with your session, typeps:

$ ps
 PID TTY           TIME CMD
 330 ttys000    0:00.00 bash
 332 ttys000    0:00:00 ps

To look a specific process, use -p and supply a PID:

$ ps -p 330
 PID TTY           TIME CMD
 330 ttys000    0:00.00 bash

To get a full listing, use -f

$ ps -f
 UID     PID  PPID   C STIME   TTY           TIME CMD
 robin   330   329   0 18:56   ttys000    0:00:04 bash
 robin   330   331   0 18:57   ttys000    0:00:04 ps

To display all processes, use -e

$ ps -e

And -ef for a full listing.

$ ps -ef | less

To display a full process listing of a specific user, use ps -fu username

$ ps -fu robin

To display root user's processes.

$ ps -fu root

To display processes in a tree format:

$ ps --forest
$ ps -ef --forest
$ pstree

To display the interactive process view, use top or htop

$ top
$ htop

Background and foreground processes

Up until this point, all the commands you have been executing have been running in the foreground. When a command, process, or program is running in the foreground, the shell prompt will not bee displayed until that process exits.

For long-running programes, it can be convenient to send them to the background. Processes that are backgrounded still execute and perform their tasks. However, they do not block you from entering further commands at the shell prompt.

To start a process in the background, place an ampersand (&) at the end of the command. To kill the current foreground process, hold down the Ctrl + c. To suspend the foreground process, type Ctrl + z. A suspended process is not running in the background. It is actually stopped.

Commands Meaning
your_command & Start command in background.
Ctrl + c Kill the foreground process.
Ctrl + z Suspend the foreground process.

To send a suspended process to the background, use bg [%job_number] command. You can background a specific job number by proceeding it with a percent sign (%) for example %12. If no job number is provided, bg will operate on the current job. The current job is considered to be the last job that was stopped while it was in the foreground, or the last job that was started in the background.

To foreground a background process, type fg [%job_number]. Similarly, the fg will operate on the current job unless you give it a specific job number.

To kill a job, type kill %job_number. Kill requires that you give a job number or a process ID number.

To list your jobs, type the jobs [%job_number].

Commands Meaning
bg [%job_number] Background a suspended process.
fg [%job_number] Foreground a background process.
kill %job_number or kill PID Kill a process by job number or PID
jobs [%job_number] List jobs.

Kill running process

If the process is running in the foreground, simply type Ctrl + c to kill it. The other way to kill a process is with the kill command. You can run kill PID to kill the process with PID. You can also specify a signal to send to the process, kill [-signal] PID. The default signal used by kill is termination. You'll see this signal referred to as SIGTERM, or TERM for short. Signals have numbers that correspond to their names. The default TERM signal is number 15. So running kill 12 is the same as running kill -15 12 or kill TERM 12.

If a process does not terminate when you send it the TERM signal, use the kill signal which is number 9, kill -9 12.

Examples

Example 1 - start a program in background

To start a program in background, simply end the command line with &

$ ./a-program &
[1] 2425

When a command is run in background, two numbers are displayed. The 1st number (in square brackets) is the job number, can be referenced by preceding it with a percent sign. The 2nd number is PID, so we can run ps -p 2425to see the process.

$ ps -p 2425
 PID TTY           TIME CMD
 330 ttys000    0:00.00 a-program
Example 2 - list jobs

Type jobs you'll see that 1 is the job number for number one

$ jobs
[1]+ Running        ./a-program &

You can also reference this by %1

$ jobs %1
[1]+ Running        ./a-program &
Example 3 - bring command to foreground and kill a foreground process

Run the fg command to bring this command to foreground

$ fg
./a-program

And to kill the foreground process, simply type Ctrl + c.

Example 4 - multi jobs

Suppose you have the 4 jobs running in the background, program-1, program-2, program-3, and program-4.

$ jobs
[1]  Running        ./program-1 &
[2]  Running        ./program-2 &
[3]- Running        ./program-3 &
[4]+ Running        ./program-4 &

The + sign represents the current job, and the - sign represents the previous job. The current job is considered to be the last job that was stopped while it was in the foreground, or the last jobs started in the background.

If no information is supplied to the fg or bg commands, then the current job is operated upon. The current jobs can be explicitly referred to by %% or %+, the previous job can be referred by %-

$ jobs %%
[4]+ Running        ./program-4 &
$ jobs %+
[4]+ Running        ./program-4 &
$ jobs %-
[3]- Running        ./program-3 &
Example 5 - kill and suspend foreground job

To bring job number 2 to the foreground:

$ fg %2
./program-2

Then, to kill it by enter Ctrl + c, then we found job number 2 has gone.

$ jobs
[1]  Running        ./program-1 &
[3]- Running        ./program-3 &
[4]+ Running        ./program-4 &

Let's bring job number 1 to foreground

$ fg %1
./program-1

Then, to suspend it by enter Ctrl + z, then we can see number one is stopped.

$ jobs
[1]+ Stopped        ./program-1 &
[3]- Running        ./program-3 &
[4]  Running        ./program-4 &