Lesson 8: Processes & Job Control
Lesson 8: Processes & Job Control
Process Basics
- PID: Process ID (unique)
- PPID: Parent Process ID
- States: R (running), S (sleeping), D (disk sleep), Z (zombie), T (stopped)
- Init (PID 1): Ancestor of all processes
Viewing Processes
ps aux # All processes (detailed)
ps aux | grep nginx # Filter
ps -ef # Standard syntax
ps -ef --forest # Tree view
pstree # Visual tree
top # Interactive (q=quit, k=kill, M=sort mem, P=sort CPU)
htop # Better top (install: apt install htop)
Process Tree
pstree -p # Show PIDs
ps -ejH # Tree with PPID
Foreground & Background
sleep 100 # Foreground (blocks shell)
sleep 100 & # Background (returns prompt, shows [jobnum] PID)
Job Control
| Command | Action |
|---|---|
jobs |
List jobs |
fg %1 |
Bring job 1 to foreground |
bg %1 |
Resume stopped job in background |
Ctrl+Z |
Suspend foreground job (SIGTSTP) |
Ctrl+C |
Kill foreground job (SIGINT) |
disown %1 |
Detach job from shell |
# Example workflow
vim file.txt # Edit
Ctrl+Z # Suspend → [1]+ Stopped
bg %1 # Resume in background
jobs # Check
fg %1 # Back to vim
Signals
| Signal | Number | Name | Default Action |
|---|---|---|---|
| SIGTERM | 15 | Terminate | Graceful exit (default for kill) |
| SIGKILL | 9 | Kill | Immediate termination (cannot catch) |
| SIGINT | 2 | Interrupt | Ctrl+C |
| SIGHUP | 1 | Hangup | Terminal closed |
| SIGSTOP | 19 | Stop | Pause (cannot catch) |
| SIGCONT | 18 | Continue | Resume |
Killing Processes
kill 12345 # SIGTERM (graceful)
kill -TERM 12345 # Explicit SIGTERM
kill -9 12345 # SIGKILL (force, last resort)
kill -KILL 12345 # Same
pkill nginx # By process name
pkill -f 'nginx: master' # By full command line
killall nginx # By exact name (all instances)
Persistent Background
# Method 1: nohup
nohup ./long-task.sh > output.log 2>&1 &
# Output goes to nohup.out (or redirected)
# Method 2: disown
./long-task.sh &
disown %1
# Method 3: screen/tmux (best for interactive)
screen -S mysession
./long-task.sh
Ctrl+A, D # Detach
screen -r mysession # Reattach
Practice
# 1. Start background sleep
sleep 200 &
jobs
# 2. Suspend and resume
vim test.txt
Ctrl+Z
bg %1
jobs
# 3. Kill gracefully then force
kill %1
jobs
# If still running:
kill -9 %1
# 4. View process tree
pstree -p | head -30
# 5. nohup test
nohup sleep 1000 > /dev/null &
disown %1
# Close terminal, reconnect — process still runs
ps aux | grep sleep
💻 实战命令速查
ps aux
List all processes (BSD syntax)
$ ps aux | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 169532 11824 ? Ss Nov01 0:05 /sbin/init
ubuntu 1234 0.0 0.0 12345 6789 pts/0 Ss 10:00 0:00 -bash
ps aux | grep nginx
Find processes by name
$ ps aux | grep nginx
root 1234 0.0 0.1 123456 7890 ? Ss 10:00 0:00 nginx: master
www-data 1235 0.0 0.2 123456 7890 ? S 10:00 0:00 nginx: worker
top
Interactive process viewer (q to quit)
$ top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 ubuntu 20 0 123456 7890 1234 R 5.0 0.5 0:01.23 htop
htop
Enhanced interactive viewer (install: apt install htop)
$ htop # F1=Help, F2=Setup, F3=Search, F4=Filter, F5=Tree, F6=Sort, F9=Kill, F10=Quit
sleep 100 &
Run command in background (&)
$ sleep 100 &
[1] 12345
$ jobs
[1]+ Running sleep 100 &
jobs
List background jobs
$ jobs
[1]- Running sleep 100 &
[2]+ Stopped vim file.txt
fg %1
Bring job 1 to foreground
$ fg %1
sleep 100
bg %1
Resume stopped job in background
$ bg %1
[1]+ sleep 100 &
kill 12345
Send SIGTERM (graceful) to PID
$ kill 12345
[1]+ Terminated sleep 100
kill -9 12345
Send SIGKILL (force) to PID
$ kill -9 12345
pkill -f 'sleep 100'
Kill by pattern (process name/command)
$ pkill -f 'sleep 100'
nohup long-script.sh &
Run immune to hangup (survives logout)
$ nohup ./backup.sh &
nohup: ignoring input and appending output to 'nohup.out'
disown %1
Remove job from shell's job table (survives logout)
$ sleep 100 &
[1] 12345
$ disown %1