How to Run Linux Commands in the Background

When you run a command in the terminal, the shell waits for it to finish before accepting the next command. This is called a foreground process. When a process runs in the foreground, it occupies your shell and you cannot run other commands until it completes.
A background process runs concurrently with the shell, freeing the terminal for other work while the command continues. This guide explains how to start commands in the background, move foreground jobs to the background, and keep processes running after the shell session ends.
Quick Reference
| Task | Command |
|---|---|
| Run command in background | command & |
| Get PID of last background job | echo $! |
| Suppress all output | command > /dev/null 2>&1 & |
| List background jobs | jobs -l |
| Bring job to foreground | fg %1 |
| Resume suspended job in background | bg %1 |
| Suspend foreground process | Ctrl+Z |
| Disown a job | disown %1 |
| Run with nohup | nohup command & |
| Detach with setsid | setsid command > output.log 2>&1 < /dev/null & |
| Wait for all background jobs | wait |
| Terminate by PID | kill PID |
Run a Command in the Background
To run a command in the background, add the ampersand symbol (&) at the end of the command:
command &The shell prints the job ID (in brackets) and the process ID (PID):
[1] 25177You can have multiple processes running in the background at the same time. To get the PID of the most recently backgrounded process, use the $! variable:
command &
echo "PID: $!"By default, the background process continues to write output to the terminal. To suppress both stdout and stderr, redirect them to /dev/null:
command > /dev/null 2>&1 &> /dev/null discards standard output, and 2>&1
redirects stderr to stdout, so both streams are silenced.
Manage Background Jobs
Use the jobs utility to list all stopped and background jobs in the current shell session:
jobs -l[1]+ 25177 Running ping google.com &The output shows the job number, PID, state, and the command that started the job.
To bring a background process to the foreground, use the fg command:
fgIf you have multiple background jobs, specify the job ID with %:
fg %1To terminate a background process, use the kill
command with the PID. Send SIGTERM first to allow the process to exit cleanly:
kill 25177If the process does not respond, force-terminate it with SIGKILL:
kill -9 25177Move a Foreground Process to the Background
To move a running foreground process to the background:
- Press
Ctrl+Zto suspend the process. The shell prints the job number and aStoppedstatus. - Run
bgto resume the process in the background:
bgIf you have multiple suspended jobs, specify which one to resume:
bg %1Run Multiple Commands in Parallel
Backgrounding several commands at once lets them run in parallel instead of one after another. Append & to each command, then use the wait built-in to block until all of them finish:
command1 &
command2 &
wait
echo "Both commands finished"wait pauses the script until every background job in the current shell completes. To wait for one specific job, pass its PID:
command1 &
pid=$!
wait "$pid"This pattern is common in scripts that start independent tasks together and then continue once all of them are done.
Keep Background Processes Running After the Shell Exits
When you close the terminal or log out, the shell sends a SIGHUP signal to all background jobs, which terminates them. Two tools prevent this: disown and nohup.
disown
disown removes a job from the shell’s job table so it no longer receives SIGHUP when the shell exits:
disownTo disown a specific job by ID:
disown %1Confirm the job was removed with jobs -l. To list all running processes including disowned ones, use ps aux
.
Note that disown does not redirect the process output. If the terminal closes, any output the process tries to write will produce an error.
nohup
The nohup
command runs a program and ignores all SIGHUP signals. It also redirects output to nohup.out automatically:
nohup command &nohup: ignoring input and appending output to 'nohup.out'If you log out or close the terminal, the process continues running. To redirect output to a specific file instead:
nohup command > output.log 2>&1 &setsid
setsid runs a command in a new session with no controlling terminal, so it is detached from the current shell from the start and is not affected by SIGHUP. Redirect input and output, then background the command so your shell prompt returns immediately:
setsid command > output.log 2>&1 < /dev/null &Unlike nohup, setsid does not redirect output automatically. If you leave output connected to the terminal, the process can still try to write there after you close the session.
Alternatives: Screen and Tmux
Terminal multiplexers create persistent sessions that survive disconnections. Unlike disown or nohup, they let you reconnect to a running session and interact with processes.
Screen
Screen (GNU Screen) is a terminal multiplexer that lets you open multiple windows inside a single session. Processes running in Screen continue even if you disconnect.
Tmux
Tmux is a modern alternative to Screen. Tmux sessions are persistent: programs running in Tmux continue even after the terminal is closed, and you can reattach to the session at any time.
Troubleshooting
bg: no current job
There is no suspended job in the current shell session. Press Ctrl+Z to suspend a running foreground process, then run bg, or specify an existing job ID such as bg %1.
There are stopped jobs when closing the terminal
The shell is warning that suspended jobs still exist. Run jobs -l to inspect them, then use fg to resume and stop them cleanly, or run bg and disown if you need them to continue.
nohup: failed to open 'nohup.out'
The current directory is not writable. Run the command from a writable directory, or explicitly set an output file: nohup command > output.log 2>&1 &.
Process stops after logout even though it was backgrounded with && alone does not protect against SIGHUP on shell exit. Use nohup command &, or run the command in Screen or Tmux when you need a persistent interactive session.
FAQ
What is the difference between disown and nohup?nohup is set before the process starts and redirects output to nohup.out. disown is applied after the process is already running and removes it from the shell’s job table. Both prevent SIGHUP from terminating the process, but nohup is safer because it also handles output.
How do I see all background processes, not just jobs in the current shell?
Use ps aux
to list all running processes, or filter by name with ps aux | grep command.
Can I run multiple commands in the background at the same time?
Yes. Append & to each command. Use jobs -l to see all running background jobs and their job IDs.
What happens to background job output if I close the terminal?
If you used & alone, the process is terminated by SIGHUP and any buffered output is lost. If you used nohup, output is saved to nohup.out. If you used disown, the process continues but may error when trying to write to the closed terminal.
How do I run a command in the background and log its output?
Redirect output to a file: command > output.log 2>&1 &. To also keep it running after logout, combine with nohup: nohup command > output.log 2>&1 &.
Conclusion
To run a command in the background, append & to it. Use disown to detach a running job from the shell, or nohup to start a process that survives logout with output saved automatically. For interactive sessions that survive disconnections, use Screen
or Tmux
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page