Today I’ll be covering job control. What exactly is job control, you ask? No, it isn’t resisting the urge to give your boss the finger every morning at 9:15am. All Linux shells have job control: the ability to run programs in the background (multitasking behind the scenes) and foreground (running as the active process at your shell prompt). A job is simply the shell’s unit of work. When you run a command interactively, your current shell tracks it as a job. When the command completes, the associated job disappears. Jobs are at a higher level than Linux processes; the Linux operating system knows nothing about them. They are merely constructs of the shell. Some important vocabulary about job control follow.

  • foreground job
    • Running in a shell, occupying the shell prompt so you cannot run another command
  • background job
    • Running in a shell, but not occupying the shell prompt, so you can run another command in the same shell
  • suspend
    • To stop a foreground job temporarily
  • resume
    • To cause a suspended job to start running again

jobs

The built-in command jobs lists the jobs running in your current shell.

$ job
[1]- Running emacs myfile &
[2]+ Stopped su

The integer on the left is the job number, and the plus sign identifies the default job affected by the fg (foreground) and bg (background) commands.

&

Placed at the end of a command line, the ampersand causes the given command to run as a background job

$ emacs myfile &
[2] 28090

The shell’s response includes the job number (2) and the process ID of the command (28090).

^Z

Typing ^Z in a shell, while a job is running in the foreground, will suspend that job. It simply stops running, but its state is remembered.

$ mybigprogram
^Z
[1]+ Stopped mybigprogram
$

Now you’re ready to type bg to put the command into the background, or fg to resume it in the foreground.

suspend

The built-in command suspend will suspend the current shell if possible, as if you’d typed ^Z to the shell itself. For instance, if you’ve run the su command and want to return to your original shell:

$ whoami
george
$ su -l
Password: ********
# whoami
root
# suspend
[1]+ Stopped su
$ whoami
george

bg [%jobnumber]

The built-in command bg sends a suspended job to run in the background. With no arguments, bg operates on the most recently suspended job. To specify a particular job (shown by the jobs command), supply the job number preceded by a percent sign:

$ bg %2

Some types of interactive jobs cannot remain in the background- for instance, if they are waiting for input. If you try, the shell will suspend the job and display:

[2]+ Stopped command line here

fg[%jobnumber]

The built-in command fg brings a suspended or backgrounded job into the foreground. With no arguments, it selects a job, usually the most recently suspended or backgrounded one. To specify a particular job (as shown by the jobs command), supply the job number preceded by a percent sign:

$ fg %2

Click Page 2 below to learn how to kill a command in progress, terminate the shell, and learn which files to edit to custom tailor your shell behavior!

Related posts: