Process management in shell scripting is essential for controlling the execution of processes,
handling multiple tasks, and managing system resources effectively. This deep dive will cover
background and foreground processes, job control, and using ps and kill commands with
examples and use cases.
1. Background and Foreground Processes
Foreground Processes: These processes run in the foreground and occupy the terminal until
they are completed. You can interact with them directly.
Background Processes: These processes run in the background, allowing you to continue using
the terminal for other commands. They can be started by appending an & at the end of the
command.
Example: Running Foreground and Background Processes
bash
Copy code
#!/bin/bash
# A script demonstrating foreground and background processes
# Run a command in the foreground
echo "Starting a foreground process..."
sleep 10 # This command will run in the foreground for 10 seconds
echo "Foreground process completed."
# Run a command in the background
echo "Starting a background process..."
sleep 10 & # This command will run in the background
echo "Background process started."
# Wait for a moment before checking background jobs
sleep 2
echo "Continuing to use the terminal while the background process runs..."
# Wait for the background job to finish
wait
echo "All processes completed."
Use Case:
Data Processing: Start a long-running data processing job in the background while
continuing to perform other tasks in the terminal.
2. Job Control (bg, fg, jobs)
Job control commands help manage background and foreground processes.
jobs: Lists all current jobs and their status.
bg: Resumes a suspended job in the background.
fg: Brings a background job to the foreground.
Example: Using Job Control Commands
bash
Copy code
#!/bin/bash
# A script demonstrating job control commands
# Start a background process
echo "Starting a background process..."
sleep 30 & # Start a sleep command in the background
job_pid=$! # Capture the PID of the last background job
# List current jobs
echo "Current jobs:"
jobs
# Suspend the background job
echo "Suspending the background job..."
kill -STOP $job_pid # Send STOP signal to suspend the job
# List jobs after suspension
echo "Jobs after suspension:"
jobs
# Bring the job back to the foreground
echo "Bringing the job back to the foreground..."
kill -CONT $job_pid # Send CONT signal to resume the job
fg %1 # Bring job with job ID 1 to the foreground
echo "Foreground process completed."
Use Case:
Task Management: Suspend a process when you need to free up terminal resources and
later resume it without losing progress.
3. Using ps and kill Commands
ps: Displays information about running processes.
kill: Sends a signal to terminate a process.
Example: Managing Processes with ps and kill
bash
Copy code
#!/bin/bash
# A script demonstrating the use of ps and kill commands
# Start a background process
echo "Starting a background process..."
sleep 60 & # Start a long-running sleep command
bg_job_pid=$! # Capture the PID of the background job
# Display running processes
echo "Current running processes:"
ps aux | grep sleep # Show processes related to sleep
# Wait a few seconds
sleep 5
# Terminate the background process
echo "Terminating the background process with PID: $bg_job_pid"
kill $bg_job_pid # Terminate the background sleep process
# Confirm termination
if ps -p $bg_job_pid > /dev/null; then
echo "Process is still running."
else
echo "Process terminated successfully."
fi
Use Case:
Resource Management: Monitor running processes to manage system resources and
terminate unresponsive or unnecessary processes to free up system resources.
Summary
1. Background and Foreground Processes:
o Run processes in the foreground or background for effective resource
management.
2. Job Control:
o Use bg, fg, and jobs commands to manage job statuses and switch between
background and foreground execution.
3. Process Management with ps and kill:
o Monitor and control running processes using ps for listing processes and kill for
terminating them.
Complete Example: Process Management Script
Here’s a complete example that showcases the concepts of process management, including
starting, suspending, and terminating processes.
bash
Copy code
#!/bin/bash
# A complete process management script
# Start a long-running background process
echo "Starting a long-running background process..."
sleep 60 & # Start a sleep command in the background
bg_job_pid=$! # Capture the PID
# Display the background job
echo "Background job PID: $bg_job_pid"
echo "Current jobs:"
jobs
# Wait for a few seconds
sleep 5
# List running processes
echo "Running processes after 5 seconds:"
ps aux | grep sleep
# Suspend the background job
echo "Suspending the background job..."
kill -STOP $bg_job_pid # Suspend the job
# Display jobs after suspension
echo "Jobs after suspension:"
jobs
# Wait for a few seconds
sleep 5
# Resume the suspended job in the foreground
echo "Resuming the job in the foreground..."
kill -CONT $bg_job_pid # Resume the job
fg %1 # Bring it to the foreground
# Confirm completion
echo "Foreground process completed."
# Terminate the process
echo "Terminating the process..."
kill $bg_job_pid # Terminate the background sleep process
# Confirm termination
if ps -p $bg_job_pid > /dev/null; then
echo "Process is still running."
else
echo "Process terminated successfully."
fi
Running the Complete Script
1. Save as process_management.sh.
2. Make it executable:
bash
Copy code
chmod +x process_management.sh
3. Execute:
bash
Copy code
./process_management.sh
This script provides a hands-on demonstration of managing processes, showcasing the practical
application of process management concepts in shell scripting.