How to run a process in the background in Linux
When a process is linked to a terminal, the process, as well as its child processes, will be terminated if the terminal is closed.
To solve this problem, the process has to be completely disconnected from its controlling terminal, i.e., run in the background.
If the process running in the background has to be moved to the foreground, use the fg command.
How to use the bg command in Linux
If an ongoing process has to be moved to the background, we can follow the steps below:
- Press
Ctrl + Zto suspend the process. - Enter the
bgcommand to resume the process in the background.
& symbol
Processes can be made to run in the background using the & symbol at the end of the command.
For example:
sleep 100 &
jobs command
The jobs command is used to list all the processes running in the background.
Example
In the code below, we ran a sleep command in the background using the & symbol at the end of the command. Then, we list all the processes running in the background.
sleep 100 &echo "All background jobs:"jobs