Run background

2 snippets across 2 stacks - Bash & Linux, Docker

Also written as run in background

SHBash & Linux

Background, Foreground & Job List

SH · Process Management
Syntax
command &
bg [%job]
fg [%job]
jobs
Example
python3 train_model.py &
jobs
fg %1
Output
[1]+  Running    python3 train_model.py &

Note Ctrl+Z suspends a foreground job. bg resumes it in the background. fg brings a background job to the foreground. jobs lists all jobs for the current shell session. %1 refers to job number 1.

DKDocker

Detached Mode (-d)

DK · Run Options
Syntax
docker run -d <image>
Example
docker run -d --name web-api -p 3000:3000 myapp:1.0
Output
e4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3...

Note Runs the container in the background and prints the container ID. Use docker logs to see its output. Without -d, your terminal is attached and Ctrl+C stops the container.

Frequently asked questions

How do you run background?
This task is covered in 2 stacks on this page: Bash & Linux, Docker. The "Background, Foreground & Job List" snippet in Bash & Linux uses `command &`.
Which command does the Bash & Linux example use?
The "Background, Foreground & Job List" snippet uses `command &`, from the Process Management section of the Bash & Linux cheat sheet.
Which stacks cover "run background" on this page?
Bash & Linux, Docker. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Background, Foreground & Job List": Ctrl+Z suspends a foreground job. bg resumes it in the background. fg brings a background job to the foreground. jobs lists all jobs for the current shell session. %1 refers to job number 1.