Linux / Unix: bg Command Examples

The bg command in Unix-like systems is used to continue executing a job in the background, after it has been stopped. When a job is stopped, it is said to be in the “stopped” state, and it will not continue executing until it is either resumed in the foreground (using the fg command) or in the background (using the bg command).

Here are some examples of how to use the bg command:

  1. Start a job in the foreground, then stop it using Ctrl-Z:
    $ sleep 100
    [1]+ Stopped sleep 100
  2. Resume the stopped job in the background using bg:
    $ bg
    [1]+ sleep 100 &
  3. Verify that the job is running in the background:
    $ jobs
    [1]+ Running sleep 100 &

In this example, the sleep command was started in the foreground, and then stopped using Ctrl-Z. The bg command was then used to resume the job in the background. The jobs command was used to verify that the job is running in the background.

Note that the bg command can also be used with a specific job ID, for example: bg %1 to resume job number 1 in the background. The jobs command can be used to list the job IDs of all jobs running in the background or stopped in the foreground.

Leave a Comment