How to Run Linux Commands in the Background

By 

Updated on

7 min read

Running commands in the background in Linux

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

TaskCommand
Run command in backgroundcommand &
Get PID of last background jobecho $!
Suppress all outputcommand > /dev/null 2>&1 &
List background jobsjobs -l
Bring job to foregroundfg %1
Resume suspended job in backgroundbg %1
Suspend foreground processCtrl+Z
Disown a jobdisown %1
Run with nohupnohup command &
Detach with setsidsetsid command > output.log 2>&1 < /dev/null &
Wait for all background jobswait
Terminate by PIDkill PID

Run a Command in the Background

To run a command in the background, add the ampersand symbol (&) at the end of the command:

Terminal
command &

The shell prints the job ID (in brackets) and the process ID (PID):

output
[1] 25177

You 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:

Terminal
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:

Terminal
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:

Terminal
jobs -l
output
[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:

Terminal
fg

If you have multiple background jobs, specify the job ID with %:

Terminal
fg %1

To terminate a background process, use the kill command with the PID. Send SIGTERM first to allow the process to exit cleanly:

Terminal
kill 25177

If the process does not respond, force-terminate it with SIGKILL:

Terminal
kill -9 25177

Move a Foreground Process to the Background

To move a running foreground process to the background:

  1. Press Ctrl+Z to suspend the process. The shell prints the job number and a Stopped status.
  2. Run bg to resume the process in the background:
Terminal
bg

If you have multiple suspended jobs, specify which one to resume:

Terminal
bg %1

Run 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:

Terminal
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:

Terminal
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:

Terminal
disown

To disown a specific job by ID:

Terminal
disown %1

Confirm 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:

Terminal
nohup command &
output
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:

Terminal
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:

Terminal
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 .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

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