Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Bash Dictionary
  3. & (Background Execution)

& (Background Execution)

Appending & to the end of a command runs it in the background. Use fg to bring a job back to the foreground, and bg to resume a stopped job in the background. To keep a process running after the terminal is closed, use nohup or disown.

Syntax

Run in the background.

command &

List jobs.

jobs
jobs -l   # Also show PIDs

Bring to the foreground.

fg
fg %job_number

Resume a stopped job in the background (stopped with Ctrl+Z).

bg
bg %job_number

Keep a process running after the terminal is closed (nohup).

nohup command &
nohup command > logfile 2>&1 &

Detach a job from the shell's job management.

disown %job_number
disown -a   # Detach all jobs

Command List

CommandDescription
command &Runs a command in the background. The PID is stored in $!.
jobsLists the background jobs of the current shell.
jobs -lDisplays the job number, PID, status, and command for each job.
fg [%N]Brings job number N to the foreground.
bg [%N]Resumes stopped job N in the background.
Ctrl+ZSuspends the foreground process (SIGSTOP).
Ctrl+CTerminates the foreground process (SIGINT).
nohup command &Keeps the process running after the terminal is closed by ignoring SIGHUP.
disown %NDetaches a process from the shell's job management.
wait [PID]Waits for the specified background process to finish.

Sample Code

Append & to a command to run it in the background, and use $! to get its PID.

bg_sample.sh
sleep 10 &
pid1=$!
echo "PID of sleep 10: $pid1"

sleep 20 &
pid2=$!
echo "PID of sleep 20: $pid2"
bash bg_sample.sh
PID of sleep 10: 12345
PID of sleep 20: 12346

Use jobs -l to check the list of background jobs.

jobs -l
[1]  12345 Running    sleep 10 &
[2]  12346 Running    sleep 20 &

Use wait to wait for all background jobs to complete.

wait_all.sh
wait
echo "All jobs done"
bash wait_all.sh
All jobs done

This example runs independent tasks in parallel in the background. The completion order depends on each task's execution time.

parallel.sh
process() {
    local name="$1"
    local secs="$2"
    sleep "$secs"
    echo "$name done (${secs}s)"
}

process "Task A" 3 &
process "Task B" 1 &
process "Task C" 2 &
wait
bash parallel.sh
Task B done (1s)
Task C done (2s)
Task A done (3s)

You can also wait for a specific PID. This is useful when you want to run a heavy task in parallel with the main process.

wait_pid.sh
long_task() {
    sleep 2
    echo "Task complete"
}

long_task &
task_pid=$!
echo "Running in background... (PID: $task_pid)"
echo "Working in main thread..."
wait $task_pid
echo "Task finished (exit status: $?)"
bash wait_pid.sh
Running in background... (PID: 12347)
Working in main thread...
Task complete
Task finished (exit status: 0)

Notes

Background jobs receive SIGHUP and terminate when the parent shell exits. To keep a process running after closing the terminal, use $ nohup command > out.log 2>&1 &. The default output file for nohup is nohup.out.

Running multiple background jobs in parallel and then waiting for all of them with $ wait is an effective pattern for parallel processing. However, error handling in shell script parallelism can become complex, so a dedicated tool is recommended for large-scale parallel workloads.

For stopping processes and sending signals, see kill / pkill / killall.

If you find any errors or copyright issues, please .