Lab 4.2 - Process Control
Objectives
By the end of this lab, you will be able to:
- Start processes
- Stop processes (kill, killall, pkill)
- Suspend and resume processes
- Manage background jobs
- Understand Linux signals
Prerequisites
- Ubuntu 24.04 installed
- Terminal open
- Normal user access
Exercise 1: Starting Processes
Step 1: Foreground Processes
# Start a simple process
sleep 60
# Start an interactive process
nano test.txt
# (Ctrl+X to exit)
Step 2: Background Processes
# Start in background with &
sleep 300 &
# See the PID
echo $!
# See the jobs
jobs
# See background processes
ps aux | grep sleep
Step 3: nohup
# Start a process that continues after logout
nohup sleep 600 > sleep.log 2>&1 &
# Verify
ps aux | grep sleep
cat sleep.log
Exercise 2: Linux Signals
Step 1: Understanding Signals
# See all signals
kill -l
# Common signals:
# SIGTERM (15): Graceful termination
# SIGKILL (9): Forced termination (cannot be ignored)
# SIGSTOP (19): Suspend
# SIGCONT (18): Resume
# SIGHUP (1): Hang up (restart)
Step 2: Sending Signals
# Start a process
sleep 300 &
PID=$!
# Send SIGTERM (graceful termination)
kill $PID
# Or with the number
kill -15 $PID
# Verify
ps -p $PID
Exercise 3: kill, killall, pkill
Step 1: kill
# Start multiple processes
sleep 100 &
PID1=$!
sleep 200 &
PID2=$!
# Kill a specific process
kill $PID1
# Kill multiple processes
kill $PID1 $PID2
# Kill with force (SIGKILL)
kill -9 $PID1
Step 2: killall
# Start multiple instances
sleep 100 &
sleep 200 &
sleep 300 &
# Kill all "sleep" processes
killall sleep
# With confirmation
killall -i sleep
# Verify
ps aux | grep sleep
Step 3: pkill
# Start processes
sleep 100 &
sleep 200 &
# Kill by name (pattern)
pkill sleep
# Kill by user
pkill -u $USER sleep
# See what will be killed (dry-run)
pkill -l sleep
Exercise 4: Suspend and Resume
Step 1: Suspend a Process
# Start a process
sleep 300 &
# Suspend with kill
kill -STOP $!
# Or with Ctrl+Z (if in foreground)
# sleep 300
# [Press Ctrl+Z]
# See the state
ps aux | grep sleep
# Note the "T" (stopped)
Step 2: Resume a Process
# Resume in background
kill -CONT $PID
# Resume in foreground
fg
# Resume in background
bg
Exercise 5: Job Management
Step 1: Job Commands
# Start multiple jobs
sleep 100 &
sleep 200 &
sleep 300 &
# See the jobs
jobs
# See with PID
jobs -l
# Bring a job to the foreground
fg %1 # Job number 1
# Move to background
bg %1
# Kill a job
kill %1
Step 2: Script with Jobs
# Create a script
cat > test-jobs.sh << 'EOF'
#!/bin/bash
sleep 10 &
JOB1=$!
sleep 20 &
JOB2=$!
echo "Jobs started: $JOB1, $JOB2"
jobs
# Wait for completion
wait $JOB1
echo "Job 1 completed"
wait $JOB2
echo "Job 2 completed"
EOF
chmod +x test-jobs.sh
./test-jobs.sh
Exercise 6: Zombie Processes
Step 1: Create a Zombie (for testing)
# Script that creates a zombie
cat > zombie.sh << 'EOF'
#!/bin/bash
# Fork a child process that dies immediately
# Parent does not wait()
(
sleep 1
exit
) &
# Parent does not wait, child becomes zombie
sleep 10
EOF
chmod +x zombie.sh
./zombie.sh &
# See the zombies
ps aux | grep -w Z
Step 2: Clean Up Zombies
# Zombies are automatically cleaned by init (PID 1)
# But can be forced by killing the parent
ps aux | grep zombie
# Find the parent PID and kill it
Exercise 7: Complete Practice
Step 1: Real Scenario
# Start a test web server
python3 -m http.server 8000 &
SERVER_PID=$!
# Verify it's running
ps -p $SERVER_PID
curl http://localhost:8000
# Stop gracefully
kill $SERVER_PID
# Verify
ps -p $SERVER_PID
Step 2: Management Script
# Script to manage a process
cat > manage-process.sh << 'EOF'
#!/bin/bash
PROCESS="python3"
ACTION=$1
case $ACTION in
start)
python3 -m http.server 8000 > /dev/null 2>&1 &
echo "Server started: $!"
;;
stop)
pkill -f "python3 -m http.server"
echo "Server stopped"
;;
status)
if pgrep -f "python3 -m http.server" > /dev/null; then
echo "Server active"
ps aux | grep "python3 -m http.server" | grep -v grep
else
echo "Server inactive"
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit 1
;;
esac
EOF
chmod +x manage-process.sh
# Use
./manage-process.sh start
./manage-process.sh status
./manage-process.sh stop
Verification
Checklist
Verify that you can:
- Start processes (foreground and background)
- Send signals (kill, killall, pkill)
- Suspend and resume processes
- Manage jobs (jobs, fg, bg)
- Understand different signals
- Manage processes practically
Reflection Questions
-
What is the difference between SIGTERM and SIGKILL?
- SIGTERM: Graceful termination (can be intercepted)
- SIGKILL: Forced termination (cannot be ignored)
-
Why use nohup?
- Allows the process to continue after terminal logout
- Useful for long-running tasks
-
What is a zombie process?
- Process that has terminated but whose parent has not waited
- Automatically cleaned by init
Next Steps
- Lab 4.3: Priorities and Nice Values
- Lab 4.4: Jobs and Background Tasks
- Chapter 4.2: Process Visualization
Lab created on: December 2024 - Tested on Ubuntu 24.04