Linux / Bash
Beginner
1 min read
Background Jobs: jobs, bg, fg, and nohup
Example
# Run a command in the background
long_running_script.sh &
# [1] 4567 — job number 1, PID 4567
# Start multiple background jobs
sleep 100 &
sleep 200 &
sleep 300 &
# List all jobs in the current session
jobs
# [1] Running sleep 100 &
# [2]- Running sleep 200 &
# [3]+ Running sleep 300 &
# '+' = current job, '-' = previous job
# Bring job 2 to the foreground
fg %2
# Suspend the foreground process
# Press: Ctrl+Z
# [2]+ Stopped sleep 200
# Resume suspended job in the background
bg %2
# Resume in the foreground
fg %2
# Kill a background job
kill %1
kill %3
# Wait for all background jobs to finish
wait
# ---- nohup: survive terminal close ----
nohup ./server.sh & # Output → nohup.out
nohup ./server.sh > /var/log/server.log 2>&1 &
# ---- disown: detach a running job ----
./server.sh &
disown %1 # Remove from job table (survives terminal close)
# ---- screen / tmux (brief reference) ----
# screen -S mysession start named session
# Ctrl+A, D detach
# screen -r mysession reattach
# tmux new -s deploy start named session
# Ctrl+B, D detach
# tmux attach -t deploy reattach
Related Resources
Linux / Bash Reference
Complete tag & property list
Linux / Bash How-To Guides
Step-by-step practical guides
Linux / Bash Exercises
Practice what you've learned
More in Linux / Bash