Introduction to Linux processes

A brief introduction to Linux processes on the CCB cluster.

What is a process?

When you execute a Linux command, the Bash session creates a process.

A process is a running instance of a program.

Every process has a process identifier (often abbreviated pid).

During execution, a process changes between states depending on its environment and circumstances. The most common states are:

  • Running – The process is actively running on the system.
  • Waiting – The process is waiting for an event to occur or for a system resource.
  • Stopped – The process has been stopped, usually by receiving a termination signal (either from the user or the system).
  • Zombie – The process has been halted but it still has an entry in the table of processes.

Foreground and background processes

Foreground

By default, command executed interactively in the Bash session of a Terminal application are run in the foreground.

For instance, the code below launches a process that does nothing but wait in the foreground for 10 seconds before terminating itself and returning the prompt to the user:

sleep 10

Running a command in the foreground.

In particular:

  • A command running in the foreground will not return the Terminal prompt to the user until the command has completed. In other words, the user cannot execute any other command until the command in the foreground has completed.
  • The standard output and standard error are printed in the Terminal application.

Background

Process can be run in the background to continue using the same Terminal instance while the process runs.

To run a command in the background, add the symbol & (ampersand) at the end of the command.

For instance, the code below launches a process that does nothing but wait 10 seconds in the background before terminating itself. During those 10 seconds, the user is free to continue using the same Terminal for launching other commmands, either in the foreground or background.

sleep 10 &

When running command in the background, it is best to redirect the standard output and standard error to files – or suppress them altogether – as the command would otherwise print its standard output and standard error in the Terminal application.

For instance:

echo "An example string" > stdout.txt 2> stderr.txt  &

In particular, the code above launches a process that:

  • Uses the echo command to print the character string "An example string" to the standard output.
  • Redirects the standard output of the command to the file stdout.txt.
  • Redirects the standard error of the command to the file stderr.txt.

Moving a process from foreground to background

To move a process from the foreground to the background:

  • First, press simultaneously the Control and Z keys to pause the process running in the foreground.
  • Then, type bg and press the Return key to send that same process to the background.

Moving a process from background to foreground

To move a process from the background to the foreground, the process depends on whether you have one or more processes running in the background.

Use the jobs command to check how many processes you are running in the background.

jobs

Running the ‘jobs’ command.

If you have a single process running in the background:

  • Type fg and press the Return key to bring that process to the foreground.

If you have more than one process running in the background:

  • Type fg followed by the integer number next to the command that you want to bring to the foreground in the output of the jobs command (e.g., fg 2).

Cheatsheet

Common options for the jobs command are listed below:

OptionDescrition
-lShow the pid in addition to the basic information.
-pOnly show the pid.
-nOnly show processes that have changed status since the last notification was printed.
-rOnly show running processes.
-sOnly show stopped processes.