0
0
Linux CLIscripting~3 mins

Background processes (&) in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to run a command but keep using the terminal for other tasks. Background processes let you do this by running commands without waiting for them to finish.
When you want to start a long download but still type other commands in the terminal.
When running a script that takes time and you want to continue working without waiting.
When launching a server or service and you want the terminal free for monitoring logs or other commands.
When testing commands that run continuously but you want to keep control of the terminal.
When automating tasks and you want multiple commands to run at the same time.
Commands
This command runs 'sleep 10' in the background, so the terminal is free immediately to run other commands.
Terminal
sleep 10 &
Expected OutputExpected
[1] 12345
Shows the list of background jobs currently running in this terminal session.
Terminal
jobs
Expected OutputExpected
[1]+ Running sleep 10
Brings the background job number 1 back to the foreground so you can interact with it or wait for it to finish.
Terminal
fg %1
Expected OutputExpected
sleep 10
Key Concept

If you remember nothing else from this pattern, remember: adding '&' after a command runs it in the background, freeing your terminal immediately.

Common Mistakes
Forgetting to add '&' at the end of the command.
The command runs in the foreground and blocks the terminal until it finishes.
Always add '&' at the end to run the command in the background.
Trying to bring a job to foreground with wrong job number.
The shell will show an error because the job number does not exist.
Use 'jobs' to check the correct job number before using 'fg %jobnumber'.
Closing the terminal without stopping background jobs.
Background jobs will be terminated when the terminal closes.
Use 'disown' or run jobs inside a terminal multiplexer if you want them to continue after closing.
Summary
Use '&' at the end of a command to run it in the background and free the terminal.
Use 'jobs' to see running background jobs and 'fg %jobnumber' to bring one back to the foreground.
Remember to manage background jobs properly to avoid losing work when closing the terminal.