Unix Job Control

Master job control in Unix. Our comprehensive guide covers background and foreground jobs, job manipulation commands, and practical examples for effective process management
E
Edtoks3:36 min read

Job control commands in Unix, such as bg, fg, and jobs, are used to manage background and foreground processes. These commands are particularly helpful when you want to work with multiple processes within a single shell session. Let's explore each of them with detailed explanations and examples:

1. bg Command (Background):

The bg command is used to move a stopped or suspended job to the background so that it continues to run but doesn't block the terminal.

Basic Usage:

bg [job_spec]
  • job_spec: The job specification, which can be a job ID or a percentage symbol (%).

Example:

$ sleep 60    # Press Ctrl+Z to suspend the sleep command
^Z
$ bg          # Move the suspended job to the background
[1]+ sleep 60 &

Explanation:

  • In the example above, the sleep 60 command is initially in the foreground and is suspended using Ctrl+Z. The bg command moves it to the background, where it continues to run.

2. fg Command (Foreground):

The fg command is used to bring a background job to the foreground, allowing you to interact with it directly through the terminal.

Basic Usage:

fg [job_spec]
  • job_spec: The job specification, which can be a job ID or a percentage symbol (%).

Example:

$ sleep 60 &   # Start a background sleep job
[1] 12345
$ fg           # Bring the background job to the foreground

Explanation:

  • In this example, sleep 60 & starts a background job, and fg is used to bring it to the foreground so that you can interact with it.

3. jobs Command:

The jobs command is used to list all the jobs that are running or suspended in the current shell session.

Basic Usage:

jobs [-l]
  • -l: Display more detailed information about jobs, including their process IDs (PIDs).

Example:

$ sleep 60 &     # Start a background sleep job
[1] 12345
$ jobs           # List all jobs
[1]+  Running                 sleep 60 &

Explanation:

  • The jobs command lists all the jobs, including their status (Running, Stopped), job IDs, and the commands associated with them.

Additional Job Control Tips:

  1. Suspend a Running Job:

    • To suspend a running job and move it to the background, press Ctrl+Z.
    • For example, while a long-running process is in the foreground, press Ctrl+Z to suspend it and then use bg to continue it in the background.
  2. Foreground and Background in One Command:

    • You can start a command in the background directly by appending & to the command, like command &.
    • To bring a background job to the foreground, simply use fg.
  3. Switching Between Jobs:

    • You can switch between multiple jobs by using their job IDs or the % symbol followed by the job number.
    • For example, fg %1 brings job 1 to the foreground, and bg %2 moves job 2 to the background.
  4. Killing a Job:

    • Use the kill command followed by the job's PID or job ID to terminate a job.
    • For example, kill %1 terminates job 1.

Job control commands are useful for managing processes in the background and foreground, which is especially valuable when working with multiple tasks in a single shell session. Understanding these commands helps you efficiently manage and control processes in a Unix-like environment.

Let's keep in touch!

Subscribe to keep up with latest updates. We promise not to spam you.