Practice extended process management techniques, including starting, suspending, and connecting to multiple concurrent tasks.
After completing this section, students should be able to:
Explain the terms foreground, background, and controlling terminal.
Use job control to manage multiple command-line tasks.
Job control is a feature of the shell which allows a single shell instance to run and manage multiple commands.
A job is associated with each pipeline entered at a shell prompt. All processes in that pipeline are part of the job and are members of the same process group. (If only one command is entered at a shell prompt, that can be considered to be a minimal "pipeline" of one command. That command would be the only member of that job.)
Only one job can read input and keyboard-generated signals from a particular terminal window at a time. Processes that are part of that job are foreground processes of that controlling terminal.
A background process of that controlling terminal is a member of any other job associated with that terminal. Background processes of a terminal can not read input or receive keyboard-generated interrupts from the terminal, but may be able to write to the terminal. A job in the background may be stopped (suspended) or it may be running. If a running background job tries to read from the terminal, it will be automatically suspended.
Each terminal is its own session, and can have a foreground process and independent background processes. A job is part of exactly one session, the one belonging to its controlling terminal.
The ps command will show the device name of the
controlling terminal of a process in the TTY column.
Some processes, such as system daemons, are
started by the system and not from a shell prompt. These processes
do not have a controlling terminal, are not members of a job, and
can not be brought to the foreground. The ps command
will display a question mark (?) in the TTY
column for these processes.
Any command or pipeline can be started in the background by appending
an ampersand (&) to the end of the command line.
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 and redisplays the shell prompt.
[student@serverX ~]$sleep 10000 &[1] 5947[student@serverX ~]$
When backgrounding a pipeline with an ampersand, the PID of the last command in the pipeline will be the one that is output. All processes in the pipeline are still members of that job.
[student@serverX ~]$example_command | sort | mail -s "Sort output" &[1] 5998
The bash shell tracks jobs, per session, in a table displayed with the jobs command.
[student@serverX ~]$jobs[1]+ Running sleep 10000 &[student@serverX ~]$
A background job can be
brought to the foreground by using the fg command
with its job ID (%job number).
[student@serverX ~]$fg %1sleep 10000 _
In the preceding example, the sleep command is now running in the foreground on the controlling terminal. The shell itself is again asleep, waiting for this child process to exit.
To send a foreground process to the background, first press the keyboard-generated suspend request (Ctrl+z) on the terminal.
sleep 10000 ^Z [1]+ Stopped sleep 10000[student@serverX ~]$
The job is immediately placed in the background and is suspended.
The ps j command will display information relating
to jobs. 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.
Since the example sleep command is currently
suspended, its process state is T.
[student@serverX ~]$ps jPPID 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[student@serverX ~]$
To start the suspended process running in the background, use the bg command with the same job ID.
[student@serverX ~]$bg %1[1]+ sleep 10000 &[student@serverX ~]$
The shell will warn a user who attempts to exit a terminal window (session) with suspended jobs. If the user tries exiting again immediately, the suspended jobs are killed.
Additional information may be available in the chapter on viewing system processes in the Red Hat Enterprise Linux System Administrator's Guide for Red Hat Enterprise Linux 7, which can be found at https://access.redhat.com/documentation/
bash info page (The GNU Bash Reference Manual)
Section 7: Job Control
libc info page (GNU C Library Reference Manual)
Section 24: Signal Handling
Section 26: Processes
bash(1), builtins(1),
ps(1), sleep(1) man pages