Chapter 4.6 - Jobs and Background Tasks
Learning Objectives
By the end of this chapter, you will be able to:
- Start background processes
- Manage jobs with jobs, fg, bg
- Use nohup for persistent processes
- Understand screen and tmux
- Manage processes after disconnection
Background Processes
Starting with &
# Background process
command &
# Example
sleep 60 &
tail -f /var/log/app.log &
# See the PID
echo $! # PID of the last background process
Redirection
# Redirect output
command > output.log 2>&1 &
# Ignore output
command > /dev/null 2>&1 &
# Example
long_script.sh > script.log 2>&1 &
Job Management
jobs - View Jobs
# List jobs
jobs
# With PIDs
jobs -l
# Also finished jobs
jobs -a
# Format:
# [1] + running sleep 60 &
# [2] - running tail -f log &
# Number, state, command
fg - Foreground
# Last job to foreground
fg
# Specific job
fg %1
fg %2
fg %name
# With Ctrl+Z, the process is suspended
# Then fg brings it to the foreground
bg - Background
# Last suspended job to background
bg
# Specific job
bg %1
bg %2
nohup - Ignore SIGHUP
Problem
When you disconnect, the shell sends SIGHUP to all its child processes, killing them.
Solution: nohup
# Ignore SIGHUP
nohup command &
# With redirection
nohup command > output.log 2>&1 &
# The nohup.out file is automatically created
# if no redirection
Features:
- Process continues after disconnection
- Output in
nohup.out(by default) - Can be redirected
screen - Persistent Sessions
Installation
sudo apt install screen # Ubuntu/Debian
sudo yum install screen # Red Hat/CentOS
Basic Usage
# Start a session
screen
screen -S session_name # With name
# Detach: Ctrl+A then D
# Reconnect: screen -r
# Reconnect named session: screen -r session_name
# List sessions
screen -ls
# Kill session
screen -X -S session_name quit
screen Commands (Ctrl+A)
Ctrl+A then:
d # Detach
c # New window
n # Next window
p # Previous window
" # List windows
? # Help
tmux - Modern Terminal Multiplexer
Installation
sudo apt install tmux # Ubuntu/Debian
sudo yum install tmux # Red Hat/CentOS
Basic Usage
# Start session
tmux
tmux new -s session_name
# Detach: Ctrl+B then D
# Reconnect: tmux attach
# Reconnect session: tmux attach -t session_name
# List sessions
tmux ls
# Kill session
tmux kill-session -t session_name
tmux Commands (Ctrl+B)
Ctrl+B then:
d # Detach
c # New window
n # Next window
p # Previous window
% # Vertical split
" # Horizontal split
? # Help
Comparison
| Tool | Usage | Advantages |
|---|---|---|
& | Simple background | Simple, quick |
nohup | Persistent processes | Continues after disconnection |
screen | Terminal sessions | Multiple windows, detachable |
tmux | Modern terminal | More features than screen |
Practical Examples
Scenario 1: Long Script
# Start in background with nohup
nohup long_script.sh > script.log 2>&1 &
# Disconnect
# The script continues
Scenario 2: Work Session
# Start screen
screen -S work
# Run multiple commands
# Detach: Ctrl+A D
# Reconnect later
screen -r work
Scenario 3: Continuous Monitoring
# In screen/tmux
watch -n 1 'ps aux --sort=-%cpu | head'
# Detach and reconnect when needed
Summary
In this chapter, you learned:
Background: command & to start in background
Jobs: jobs, fg, bg to manage jobs
nohup: Ignore SIGHUP, persistent processes
screen: Detachable terminal sessions
tmux: Modern terminal multiplexer
Practice: Choose according to needs (simple vs sessions)
Next Steps
Module 5: Users and Permissions
Module 6: Shell Scripting
Chapter created on: December 2024