This tutorial provides a step-by-step guide to understanding and managing processes in Linux. You’ll learn how to view, control, and optimize processes, as well as troubleshoot common issues. By the end, you’ll be equipped with practical skills for real-world system administration tasks.
Step 1: Understanding Linux Processes
A process is an instance of a running program in Linux. Each process has a unique Process ID (PID) and is managed by the kernel. Processes can be in various states, such as running, sleeping, or stopped.
- Foreground processes: Run in the terminal and require user interaction.
- Background processes: Run independently of the terminal.
Use the ps command to view active processes:
ps auxPractice Exercise
Use the ps command to list all processes running under your user. Identify the PID of a specific process (e.g., your terminal).
Show Solution
ps -u $USER
# Look for the terminal process (e.g., bash or zsh) and note its PID.Step 2: Starting and Stopping Processes
You can start a process in the foreground or background. Use & to run a process in the background.
- To stop a foreground process, press Ctrl+C.
- To pause a process, press Ctrl+Z.
- Use kill to terminate a process by its PID.
sleep 100 &
killPractice Exercise
Start a process in the background (e.g., sleep 200). Then, stop it using its PID.
Show Solution
sleep 200 &
kill $(pgrep sleep)
# `pgrep` finds the PID of the `sleep` process.Step 3: Monitoring Processes with `top` and `htop`
The top command provides a real-time view of system processes, including CPU and memory usage. htop is an enhanced version with a more user-friendly interface.
- Use top to monitor processes.
- Use htop for interactive process management (install with sudo apt install htop).
top
htopPractice Exercise
Use htop to identify the process consuming the most CPU. Terminate it using the F9 key.
Show Solution
htop
# Navigate to the process with the highest CPU usage, press `F9`, and select `SIGKILL`.Step 4: Managing Process Priorities with `nice` and `renice`
Process priority determines how much CPU time a process gets. Use nice to start a process with a specific priority (range: -20 to 19, where -20 is highest). Use renice to change the priority of a running process.
nice -n 10 sleep 100
renice -n 5 -pPractice Exercise
Start a process with a low priority (e.g., nice -n 19 sleep 300). Then, increase its priority using renice.
Show Solution
nice -n 19 sleep 300 &
renice -n 10 -p $(pgrep sleep)
# Verify the priority change using `ps -l`.Step 5: Using `nohup` and `disown` for Persistent Processes
To keep a process running after closing the terminal, use nohup or disown.
- nohup runs a process immune to hangup signals.
- disown removes a background job from the shell's job table.
nohup sleep 100 &
disownPractice Exercise
Start a long-running process (e.g., sleep 500) with nohup. Close the terminal and verify the process is still running.
Show Solution
nohup sleep 500 &
# Close the terminal, then check with `ps aux | grep sleep`.Step 6: Process Signals and `kill` Command
Signals are used to communicate with processes. Common signals include:
- SIGTERM (15): Gracefully terminate a process.
- SIGKILL (9): Forcefully terminate a process.
Use kill -l to list all signals.
kill -9Practice Exercise
Start a process (e.g., sleep 100). Send it a SIGTERM signal, then a SIGKILL signal if it doesn't terminate.
Show Solution
sleep 100 &
kill -15 $(pgrep sleep)
# If it doesn't terminate:
kill -9 $(pgrep sleep)Step 7: Job Control with `jobs`, `fg`, and `bg`
Jobs are processes managed by the shell. Use jobs to list background jobs, fg to bring a job to the foreground, and bg to resume a stopped job in the background.
sleep 100 &
jobs
fg %1
bg %1Practice Exercise
Start two background jobs (e.g., sleep 100 and sleep 200). Bring the first job to the foreground, then pause it and resume it in the background.
Show Solution
sleep 100 &
sleep 200 &
jobs
fg %1
# Press `Ctrl+Z` to pause, then:
bg %1Step 8: Process Scheduling with `cron`
cron is a time-based job scheduler. Use crontab to create and manage cron jobs.
- Edit the crontab file: crontab -e.
- Schedule a job: * * * * * command (minute, hour, day, month, weekday).
crontab -e
* * * * * echo 'Hello, World!' >> /tmp/cron_test.txtPractice Exercise
Create a cron job that appends the current date to a file every minute. Verify the output after a few minutes.
Show Solution
crontab -e
* * * * * date >> /tmp/cron_test.txt
# Wait a few minutes, then check:
cat /tmp/cron_test.txtStep 9: Troubleshooting Process Issues
Common issues include high CPU/memory usage, zombie processes, and unresponsive processes.
- Use ps aux to identify resource-heavy processes.
- Use kill to terminate unresponsive processes.
- Zombie processes are handled by the kernel and usually resolve themselves.
ps aux | grep -i 'defunct'Practice Exercise
Simulate a high CPU usage process (e.g., while true; do :; done). Identify and terminate it using top or htop.
Show Solution
while true; do :; done &
# Use `top` or `htop` to find the process, then:
kill -9 $(pgrep -f 'while true')Sign in to take Cornell notes on this lesson — they save automatically and stay with your account.