
The Process Master: ps, kill, and nice
Take control of the running programs on your system. Learn to track PIDs with ps, manage frozen apps with kill and pkill, and adjust the 'niceness' of background tasks. Understand the critical difference between SIGTERM and SIGKILL.
Process Management: Controlling the Workflow
In Linux, every running program—from the smallest command to the largest database—is a Process. Each process is assigned a unique tracking number called a PID (Process ID).
As an administrator, you are the conductor of this orchestra. Sometimes a musician gets out of sync (a memory leak), and you need to stop them. Sometimes a task is too loud (takes too much CPU), and you need to tell them to be "quieter" (lower their priority).
In this lesson, we will learn how to identify, stop, and prioritize processes.
1. ps: High-speed Snapshots
ps (Process Status) gives you a snapshot of current processes.
The "aux" Combo (The Industry Standard)
Most pros just type ps aux.
a: All users.u: User-oriented format (shows CPU/MEM %).x: Also show processes not started in a terminal (background services).
# Find any running process containing the word 'python'
ps aux | grep python
2. Managing Life and Death: kill and pkill
When a process is frozen or misbehaving, you need to send it a "Signal."
The Terminology of Signals:
- SIGTERM (15): The "Please Stop" signal. It allows the program to save its data and close gracefully. Try this first!
- SIGKILL (9): The "Execute Immediately" signal. The kernel kills the process instantly. No saving, no cleanup. Only use this as a last resort.
Using kill (By PID):
# Send a graceful termination (default)
kill 1234
# Force kill (unblockable)
kill -9 1234
Using pkill (By Name):
# Kill every process named 'chrome'
pkill chrome
3. Nice: The Art of Priority
Every process has a "Niceness" value ranging from -20 (Highest Priority) to 19 (Lowest Priority).
By default, processes have a niceness of 0.
- If you want to run a massive data compression task in the background without slowing down your browser, make it "Nice."
# Start a program with low priority (High niceness)
nice -n 15 tar -czf backup.tar.gz /data
# Change the priority of an ALREADY running process
renice -n 5 -p 1234
4. Foregound vs. Background
Suppose you started a long task (like a scan) and you forgot to add & to make it a background task.
- Ctrl + Z: "Pause" the process and return to the terminal.
bg: Send that paused process to the Background to continue running.fg: Bring it back to the Foreground.
5. Practical: Finding the "Stuck" App
If you have a website that isn't loading, but the server looks okay, check the "State" (STAT) column of ps aux:
R: Running (Actually busy).S: Sleeping (Waiting for something).D: Uninterruptible Sleep (Waiting for the disk). If you see a lot of D's, your hard drive is dying or overloaded.Z: Zombie (Finished, but its parent hasn't acknowledged it yet).
6. Example: A Process Resource Watchdog (Python)
Imagine you want a script that automatically kills any process that uses more than 50% CPU for more than 10 seconds. Here is a Python script that implements that logic using psutil.
import psutil
import time
def terminate_resource_hogs(cpu_threshold=50.0):
"""
Scans processes and terminates those exceeding CPU thresholds.
"""
print(f"Monitoring for CPU hogs (> {cpu_threshold}%)...")
for proc in psutil.process_iter(['pid', 'name', 'cpu_percent']):
try:
# We must call cpu_percent twice to get a real reading
# but for a scan, we rely on the system-wide average
cpu = proc.info['cpu_percent']
if cpu > cpu_threshold:
print(f" [!] Found hog: {proc.info['name']} (PID: {proc.info['pid']}) @ {cpu}%")
# Attempt to terminate gracefully
proc.terminate()
print(f" [X] Sent SIGTERM to {proc.info['pid']}")
except (psutil.NoSuchProcess, psutil.AccessDenied):
continue
if __name__ == "__main__":
# In a real environment, you'd run this in a loop
try:
terminate_resource_hogs()
except Exception as e:
print(f"Error: {e}")
7. Professional Tip: Check 'Process Tree' (pstree)
If you have a server running thousands of tasks, it's hard to tell what belongs to what. Use pstree to see a visual chart of the "Parent-Child" relationships. You'll see systemd at the top, spawning everything else.
8. Summary
Process management is the core of system control.
ps auxshows you the landscape.kill -15is for manners;kill -9is for emergencies.niceandrenicemanage your system's "politeness."Ctrl+Zandbgmanage your multi-tasking workflow.
In the next lesson, we will look at Real-time Performance Analysis with the expert tools vmstat and iostat.
Quiz Questions
- What does a "Zombie" process signify in Linux?
- What is the difference between
kill 1234andkill -9 1234? - How can you find the PID of a specific program (like
nginx) without usingps aux | grep nginx? (Hint: checkpgrep)
Continue to Lesson 5: Real-time Performance Analysis—vmstat and iostat.