Typically when you run a command in the terminal, you have to wait until the command finishes before you can enter another one. This is called running the command in the foreground or foreground process. When a process runs in the foreground, it occupies your shell, and you can interact with it using the input devices.
What if the command takes a long time to finish, and you want to run other commands in the meantime? You have several options at your disposal. The most obvious and straightforward choice is to start a new shell session and run the command in it. Another option is to run the command in the background.
A background process is a process/command that is started from a terminal and runs in the background without interaction from the user.
In this article, we will talk about the background processes is Linux. We will show you how to start a command in the background and how to keep the process running after the shell session is closed.
Run a Linux Command in the Background
To run a command in the background, add the ampersand symbol (&
) at the end of the command:
command &
The shell job ID (surrounded with brackets) and process ID will be printed on the terminal:
[1] 25177
You can have multiple processes running in the background at the same time.
The background process will continue to write messages to the terminal from which you invoked the command. To suppress the stdout
and stderr
messages use the following syntax:
command > /dev/null 2>&1 &
>/dev/null 2>&1
means redirect stdout
to /dev/null
and stderr
to stdout
.
Use the jobs
utility to display the status of all stopped and background jobs in the current shell session:
jobs -l
The output includes the job number, process ID, job state, and the command that started the job:
[1]+ 25177 Running ping google.com &
To bring a background process to the foreground, use the fg
command:
fg
If you have multiple background jobs, include %
and the job ID after the command:
fg %1
To terminate the background process, use the kill
command followed by the process ID:
kill -9 25177
Move a Foreground Process to Background
To move a running foreground process in the background:
- Stop the process by typing
Ctrl+Z
. - Move the stopped process to the background by typing
bg
.
Keep Background Processes Running After a Shell Exits
If your connection drops or you log out of the shell session, the background processes are terminated. There are several ways to keep the process running after the interactive shell session ends.
One way is to remove the job from the shell’s job control using the disown
shell builtin:
disown
If you have more than one background jobs, include %
and the job ID after the command:
disown %1
Confirm that the job is removed from the table of active jobs using the jobs -l
command. To list all running processes, including the disowned, use the ps aux
command.
Another way to keep a process running after the shell exit is to use nohup
.
The nohup
command executes another program specified as its argument and ignores all SIGHUP
(hangup) signals. SIGHUP
is a signal that is sent to a process when its controlling terminal is closed.
To run a command in the background using the nohup
command, type:
nohup command &
The command output is redirected to the nohup.out
file.
nohup: ignoring input and appending output to 'nohup.out'
The process is not terminated if you log out or close the terminal.
Alternatives
There are several programs that allow you to have multiple interactive sessions at the same time.
Screen
Screen or GNU Screen is a terminal multiplexer program that allows you to start a screen session and open any number of windows (virtual terminals) inside that session. Processes running in Screen will continue to run when their window is not visible even if you get disconnected.
Tmux
Tmux is a modern alternative to GNU screen. With Tmux, you can also create a session and open multiple windows inside that session. Tmux sessions are persistent, which means that programs running in Tmux continue to run even if you close the terminal.
Conclusion
To run a command in the background, include &
at the end of the command.
When you run a command in the background, you don’t have to wait until it finishes before you can execute another one.
If you have any questions or feedback, feel free to leave a comment.