Job Control Commands in Linux | bg, fg, and CTRL+Z

In the Linux operating system, the command shell manages a series of processes known as “jobs.” These jobs are assigned to the shell for execution with three possible states such as “background,” “foreground,” and “stopped.” Linux provides the “jobs” command line tool that interacts directly with the system jobs and allows users to list and manage their states. 

This guide describes Linux’s job control commands bg, fg, and CTRL+Z.

  • Working of the “bg” Command
  • Working of the “fg” Command
  • Working of the “Ctrl+Z” Shortcut Key

How Does the “bg” Command Work in Linux?

The “bg” is the short form of the “background” job control command that switches the foreground processes into the background. The process stops for the time being and then resumes in the background.

Syntax:

The generalized syntax of the “bg” command is written here:

$ bg [job]

The syntax of the “bg” command supports only one parameter, “job.” This parameter can be denoted in four ways:

  • %n: Specifies the job number or ID.
  • %% or %+: Send the current process in the background.
  • %-: Places the previous process in the background.
  • %string: Identifies the process that starts from the defined string.

Let’s see with an example how to use the “bg” command to place the foreground process in the background.

Example:

A “sleep” command is executed for “40” seconds. After that, press the “Ctrl+Z” key to stop this job:

$ sleep 40

The output shows that the “sleep” command has now been stopped having job ID [1].

Execute the “jobs” command to list down the background jobs:

$ jobs

To keep the “sleep” command in the background, use the “bg” command with an argument “%1(job ID)” in this way: 

$ bg %1                                   #Place Forground process in background
$ jobs                                    # List all jobs

The execution of the “jobs” command confirms the background “sleep” command is in “Running” state. The “&(ampersand)” symbol denotes that “sleep” is a background process.

How Does the “fg” Command Work in Linux?

The “fg” command is opposite to the “bg” command. It is an acronym of the “foreground” jobs control command utilized to resume the background job in the foreground.

Syntax:

The “fg” command follows this basic syntax:

$ fg [job]

In the above syntax, the user can pass the %n, %% or %+,%-, and the %string parameter in place of “[job]” same as the “bg” command.

 If no parameter is passed with the “fg” command, it will print the most recent background process.

Example:

List down the jobs that are running in the background using the “jobs” command:

$ jobs

The output contains all the jobs list from which only the first background job, i.e., the “sleep 500” command, is in the “Running” state.

Execute the “fg” command having the “sleep” command job ID “%1” to keep it in the foreground:

$ fg %1

The “fg” command has successfully moved the “sleep” command to the foreground. It will take time to complete its execution, and no other command can be executed until its completion.

How Does the “Ctrl+Z” Shortcut Key Work in Linux?

The “Ctrl+Z” is the key to stopping the foreground process and keeping it in the “stopped” state. It can be used at any point of the process/command execution. It is not a command and is a shortcut key in the shell.

Example:

There is a foreground process, i.e., installation of the “curl” command line tool. Press the “Ctrl+Z” to stop it:

$ sudo apt install curl

The above “apt” command has been stopped. 

Run the “jobs” command to verify the stopped process:

$ jobs

The output shows that the “sudo apt install curl” foreground process has been stopped. 

Conclusion

The “jobs” control command “bg” places the foreground job in the background, and the “fg” resumes the background jobs. Moreover, the “Ctrl+Z” shortcut key keeps the job in a “stopped” state. 

All these “job control” utilities handle the jobs directly through the terminal.

This guide has provided a detailed view of jobs control commands “bg,” “fg” and the “Ctrl+Z” in Linux.