🎁 New User? Get 20% off your first purchase with code NEWUSER20 Register Now →
Menu

Categories

Linux Process Management: ps, top, htop, and systemctl Explained

Linux Process Management: ps, top, htop, and systemctl Explained

Understanding how to manage processes is a fundamental skill for any Linux administrator. Whether you are troubleshooting a slow server, tracking down memory leaks, or managing system services, knowing the right tools and commands is essential.

Understanding Linux Processes

Every running program on a Linux system is a process. Each process has a unique Process ID (PID), a parent process (PPID), resource allocations, and a state. The init system (PID 1) is the ancestor of all processes on the system.

The ps Command

The ps command provides a snapshot of current processes:

# Show all processes with full details
ps aux

# Show process tree
ps auxf

# Show processes for a specific user
ps -u www-data

# Find a specific process
ps aux | grep nginx

# Show only PID, user, CPU, memory, and command
ps -eo pid,user,%cpu,%mem,cmd --sort=-%mem | head -20

Real-Time Monitoring with top

The top command provides a dynamic, real-time view of running processes:

# Launch top
top

# Useful keyboard shortcuts inside top:
# M - Sort by memory usage
# P - Sort by CPU usage
# k - Kill a process (enter PID)
# c - Show full command line
# 1 - Show per-CPU statistics
# q - Quit

Enhanced Monitoring with htop

htop is an improved version of top with a user-friendly interface:

# Install htop
sudo apt install htop  # Debian/Ubuntu
sudo dnf install htop  # RHEL/Fedora

# Launch htop
htop

# Features over top:
# - Color-coded output
# - Mouse support
# - Horizontal and vertical scrolling
# - Tree view (F5)
# - Search processes (F3)
# - Filter processes (F4)
# - Kill processes (F9)
# - Nice value adjustment (F7/F8)

Managing Services with systemctl

systemctl is the primary tool for managing systemd services:

# Start, stop, restart a service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx

# Reload configuration without restart
sudo systemctl reload nginx

# Enable/disable service at boot
sudo systemctl enable nginx
sudo systemctl disable nginx

# Check service status
systemctl status nginx

# List all running services
systemctl list-units --type=service --state=running

# List failed services
systemctl list-units --type=service --state=failed

# View service logs
journalctl -u nginx -f --since "1 hour ago"

Process Signals

Control processes using signals:

# Graceful termination
kill -SIGTERM 12345
kill -15 12345

# Force kill (last resort)
kill -SIGKILL 12345
kill -9 12345

# Reload configuration
kill -SIGHUP 12345

# Kill all processes by name
killall nginx
pkill -f "python app.py"

Process Priority and Nice Values

Adjust process scheduling priority with nice values (-20 highest to 19 lowest):

# Start process with lower priority
nice -n 10 ./backup-script.sh

# Change priority of running process
renice -n 5 -p 12345

# Start with highest priority (requires root)
sudo nice -n -20 ./critical-task

Troubleshooting Common Issues

  • High CPU usage: Use top sorted by CPU (press P) to identify the culprit
  • Memory leaks: Monitor RSS and VSZ columns in ps aux over time
  • Zombie processes: Find with ps aux | grep Z and investigate the parent process
  • Orphan processes: Look for processes whose PPID is 1 unexpectedly
  • Disk I/O bottleneck: Use iotop to identify I/O-heavy processes

Mastering process management makes you a more effective system administrator. Practice these commands regularly and they will become second nature when troubleshooting production issues.

Share this article:

Stay Updated

Subscribe to our newsletter for the latest tutorials, tips, and exclusive offers.