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.

Linux & Mac & Bash Command Dictionary

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

& (Background Execution)

Since: All Linux
macOS(2001 Cheetah)
Bash 1.0(1989)

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.

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

sleep 20 &
pid2=$!
echo "PID of sleep 20: $pid2"

Run the following command:

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.

sample_wait_all.sh
wait
echo "All jobs done"

Run the following command:

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.

sample_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

Run the following command:

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.

sample_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: $?)"

The following example demonstrates this:

bash wait_pid.sh
Running in background... (PID: 12347)
Working in main thread...
Task complete
Task finished (exit status: 0)

Common Mistakes

Common Mistake 1: Forgetting & and running the command in the foreground

Without &, the command runs in the foreground and the shell waits for it to finish before accepting the next input.

sleep 60
(the terminal is blocked for 60 seconds)

Append & to run it in the background.

sleep 60 &
[1] 12345

Common Mistake 2: Process terminates when the terminal is closed

Background jobs are children of the shell. When the terminal is closed, SIGHUP is sent to child processes and they terminate.

./long_task.sh &
(closing the terminal sends SIGHUP and the process terminates)

Use nohup to keep the process running after the terminal closes.

nohup ./long_task.sh > out.log 2>&1 &

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 .