Bookmark this page

Control Jobs

Objectives

Use Bash job control to manage multiple processes that were started from the same terminal session.

Describe Jobs and Sessions

With the job control shell feature, a single shell instance can run and manage multiple commands.

A job is associated with each pipeline that is entered at a shell prompt. All processes in that pipeline are part of the job and are members of the same process group. You can consider a minimal pipeline to be only one command that is entered in the shell prompt to create a job with only one member.

Only one job at a time can read input and keyboard-generated signals from a particular terminal window. Processes that are part of that job are foreground processes of that controlling terminal.

A background process of that controlling terminal is any other job that is associated with that terminal. Background processes of a terminal cannot read input or receive keyboard-generated interrupts from the terminal, but can write to the terminal. A background job might be stopped (suspended) or it might be running. If a running background job tries to read from the terminal, then it is automatically suspended.

Each terminal runs in its own session, and can have a foreground process and any number of background processes. A job that runs in its own session belongs to its controlling terminal.

The ps command shows the device name of the controlling terminal in the TTY column. Some processes, such as system daemons, are started by the system and not from a controlling terminal. These processes are not members of a job, and cannot be brought to the foreground. The ps command displays a question mark (?) in the TTY column for these processes.

Run Jobs in the Background

Any command or pipeline can be started in the background by appending an ampersand (&) character to the command. The Bash shell displays a job number (unique to the session) and the PID of the new child process. The shell does not wait for the child process to terminate, but instead displays the shell prompt.

[user@host ~]$ sleep 10000 &
[1] 5947
[user@host ~]$

When a command line with a pipe (|) is sent to the background, the PID of the last command in the pipeline is displayed. All pipeline processes are members of that job.

[user@host ~]$ example_command | sort | mail -s "Sort output" &
[1] 5998

Use the jobs command to display the list of jobs for the shell's session.

[user@host ~]$ jobs
[1]+ Running    sleep 10000 &
[user@host ~]$

Use the fg command to bring a background job to the foreground. Use the (%jobNumber) format to specify the process to foreground.

[user@host ~]$ fg %1
sleep 10000

In the preceding example, the sleep command is running in the foreground on the controlling terminal. The shell itself is asleep and waiting for this child process to exit.

To send a foreground process to the background, press the keyboard-generated suspend request (Ctrl+z) in the terminal. The job is placed in the background and suspended.

[user@host ~]$ sleep 10000
^Z
[1]+  Stopped                 sleep 10000
[user@host ~]$

The ps j command displays information about jobs. Use the ps j command to find process and session information.

  • The PID is the unique process ID of the process.

  • The PPID is the PID of the parent process of this process, the process that started (forked) it.

  • The PGID is the PID of the process group leader, normally the first process in the job's pipeline.

  • The SID is the PID of the session leader, which (for a job) is normally the interactive shell that is running on its controlling terminal.

In the next example, the sleep command is currently suspended and the process state is T.

[user@host ~]$ ps j
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
 2764  2768  2768  2768 pts/0     6377 Ss    1000   0:00 /bin/bash
 2768  5947  5947  2768 pts/0     6377 T     1000   0:00 sleep 10000
 2768  6377  6377  2768 pts/0     6377 R+    1000   0:00 ps j

Use the bg command with the job ID to start running the suspended process.

[user@host ~]$ bg %1
[1]+ sleep 10000 &

The shell warns a user who attempts to exit a terminal window (session) with suspended jobs. If the user tries again to exit immediately, then the suspended jobs are killed.

Note

In the previous examples, the + sign indicates that this job is the current default. If a job-control command is used without the %jobNumber argument, then the action is taken on the default job. The - sign indicates the previous job that will become the default job when the current default job finishes.

 

References

Bash info page (The GNU Bash Reference Manual) https://www.gnu.org/software/bash/manual

  • Section 7: Job Control

bash(1), builtins(1), ps(1), and sleep(1) man pages

Revision: rh124-9.0-398f302