Loading...

Linux Process Management: ps, top, htop, kill, bg, fg, and Process Priorities

Back to blog
LinuxProcessesSystem AdministrationPerformance

Linux Process Management: ps, top, htop, kill, bg, fg, and Process Priorities

Master Linux process management including viewing processes with ps, top, htop, sending signals with kill, background/foreground jobs, nice, and understanding process states.

9 min read

Linux Process Management: ps, top, htop, kill, and Process Priorities

Process management is essential for system administration, troubleshooting, and performance optimization. Understanding how to view, prioritize, and control processes is fundamental to Linux expertise.

Viewing Processes

ps - Process Status

View snapshot of running processes.

# Simple process list
ps
# PID TTY TIME CMD
# 1234 pts/0 00:00:00 bash
 
# All processes (all users, all terminals)
ps aux
# USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
# root  1   0.0  0.1 19612 3556 ?   Ss  Jan18 0:05 /sbin/init
 
# List with full format
ps -ef
# UID  PID PPID C STIME TTY TIME CMD
# root  1   0    0 Jan18 ?   00:05 /sbin/init
 
# Find specific process
ps aux | grep nginx
# www-data 1234 0.0 0.2 12345 2048 ? S 10:00 0:00 nginx
 
# Process tree hierarchy
ps -ef --forest
# Shows parent-child relationships
 
# Show threads
ps -eLf
# Shows lightweight processes (threads)
 
# Process by user
ps -u john
# Only john's processes
 
# Processes using most CPU/Memory
ps aux --sort=-%cpu | head
# Sorted by CPU usage
 
ps aux --sort=-%mem | head
# Sorted by memory usage

ps Output Fields

# Common fields explained:
# USER: Process owner
# PID: Process ID (unique identifier)
# %CPU: CPU usage percentage
# %MEM: Memory usage percentage
# VSZ: Virtual memory size (KB)
# RSS: Resident set size (physical memory, KB)
# STAT: Process state (S=sleeping, R=running, Z=zombie, T=stopped)
# START: Process start time
# TIME: CPU time consumed
# COMMAND: Command executed
 
# Get custom output
ps -o pid,user,cpu,mem,comm
# PID USER %CPU %MEM COMMAND
# 1234 john 0.5 2.3 bash

top - Interactive Process Monitor

Real-time process monitoring and resource usage.

# Start top
top
# Press 'q' to quit
 
# Show processes for user
top -u john
 
# Batch mode (non-interactive)
top -b -n 1
# -b: batch mode
# -n: number of iterations
 
# Show specific process
top -p 1234
# Only PID 1234
 
# Delay updates (seconds)
top -d 2
# Update every 2 seconds
 
# Inside top, useful commands:
# q: quit
# f: change displayed fields
# F: filter by field
# k: kill process
# r: renice process
# u: filter by user
# M: sort by memory
# P: sort by CPU
# T: sort by time
# R: reverse sort

htop - Enhanced Process Monitor

Advanced interactive process monitor (install with: sudo apt-get install htop).

# Start htop
htop
 
# Show processes for user
htop -u john
 
# Show tree view
htop -t
# -t: tree view
 
# Search for process
# In htop: type /process_name
 
# Custom display
htop -p 1234,5678
# Show specific PIDs
 
# Columns:
# PID: Process ID
# USER: Process owner
# VIRT: Virtual memory
# RES: Physical memory
# CPU%: CPU usage
# MEM%: Memory percentage
# TIME+: CPU time consumed
# COMMAND: Command
 
# htop advantages:
# - Colored output
# - User-friendly interface
# - Tree view
# - Easy process killing
# - Memory monitoring

pstree - Process Tree

Display processes in hierarchical format.

# Show process tree
pstree
 
# Show with PID
pstree -p
 
# Show with CPU/memory info
pstree -p -u
 
# Show specific process tree
pstree -p 1234
# Shows descendants of PID 1234
 
# Show user's processes
pstree john
 
# Show numeric users (UID instead of username)
pstree -u
 
# Show process command line
pstree -a

atop - Advanced Top

Detailed system and process monitoring.

# Install atop
sudo apt-get install atop
 
# Start atop
atop
 
# Record system statistics
sudo atop -w /tmp/atop.log
 
# Replay recorded data
atop -r /tmp/atop.log
 
# Show specific interval
atop -r /tmp/atop.log -g

Process Signals and Control

kill - Send Signals

Terminate or signal processes.

# Send SIGTERM (graceful termination)
kill 1234
 
# Send SIGKILL (force kill)
kill -9 1234
kill -KILL 1234
 
# Send SIGSTOP (pause process)
kill -STOP 1234
 
# Send SIGCONT (resume process)
kill -CONT 1234
 
# Send SIGHUP (reload configuration)
kill -HUP 1234
 
# Kill all processes matching name
killall apache2
killall -9 firefox
 
# Kill process group
kill -15 -1234
# - (minus sign) = process group
 
# Common signals:
# SIGHUP (1): Hangup
# SIGINT (2): Interrupt (Ctrl+C)
# SIGTERM (15): Termination signal (default)
# SIGKILL (9): Kill (cannot be caught)
# SIGSTOP (19): Stop
# SIGCONT (18): Continue
 
# List available signals
kill -l
 
# Kill with custom signal
kill -USR1 1234
 
# Kill multiple processes
kill 1234 5678 9012
 
# Kill processes of user
sudo killall -u john

pkill - Kill by Pattern

Kill processes matching pattern.

# Kill by process name
pkill nginx
 
# Kill by process name (exact)
pkill -x bash
 
# Kill by user
pkill -u john
 
# Kill by terminal
pkill -t pts/0
 
# Kill with signal
pkill -STOP nginx
pkill -CONT nginx
 
# Show what would be killed
pgrep -l nginx
 
# Full command matching
pgrep -f "python script.py"

Foreground and Background Jobs

Running in Background

# Run process in background
long_running_command &
# [1] 1234
 
# View background jobs
jobs
# [1]+ Running    long_running_command &
 
# Bring to foreground
fg
# long_running_command
 
# Control-Z to stop current process
# (Press Ctrl+Z)
# [1]+ Stopped    long_running_command
 
# Resume in background
bg
# [1]+ long_running_command &
 
# Resume in foreground
fg
# long_running_command
 
# Multiple background jobs
command1 &
command2 &
command3 &
jobs
# [1] Running    command1 &
# [2] Running    command2 &
# [3] Running    command3 &
 
# Bring specific job to foreground
fg %2
# Brings job 2 to foreground
 
# Terminate background job
kill %1
# Kills job 1

nohup - Immune to Hangups

Run process immune to terminal disconnection.

# Run with nohup (immune to terminal closure)
nohup long_running_script.sh &
# Appends output to nohup.out
 
# Redirect output
nohup ./script.sh > output.log 2>&1 &
 
# Check if running
ps aux | grep script.sh
 
# Kill nohup process
kill 1234
 
# View output
tail -f nohup.out

disown - Detach from Shell

Detach job from shell session.

# Start job
sleep 1000 &
# [1] 1234
 
# Remove from job table
disown %1
 
# View jobs (1 is gone)
jobs
# (no output)
 
# Process continues running
ps aux | grep sleep
# Process still exists

Process Priority and Resource Allocation

nice - Set Process Priority

# Run with nice value (lower = higher priority)
nice -n 10 command
# -n: nice value (-20 to 19)
 
# Higher nice value (lower priority)
nice -n 15 backup_script.sh
 
# No nice value (default 0)
nice command
 
# Negative nice (requires sudo, increases priority)
sudo nice -n -10 important_service
 
# View current nice value
ps -o pid,user,nice,comm
# PID USER NI COMMAND
# 1234 john 0 bash

renice - Change Process Priority

# Change existing process priority
renice 10 -p 1234
# Increases nice value of PID 1234 to 10
 
# Lower priority for process (increase nice)
renice 15 -p 1234
 
# Increase priority (requires sudo)
sudo renice -10 -p 1234
 
# Change priority for user's processes
renice 10 -u john
# All john's processes get nice 10
 
# Change priority for group
renice 10 -g developers
 
# View process with custom priority
ps -o pid,user,nice,cmd | grep script
# 1234 john 10 script.sh

Process Information and Status

Process States

# View process states with ps
ps aux
 
# Common states:
# S: Interruptible sleep
# R: Running
# Z: Zombie (finished but parent hasn't read status)
# T: Stopped
# W: Paging
# D: Disk sleep (uninterruptible)
# <: High priority
# N: Low priority
# L: Has pages locked in memory
# s: Session leader
# l: Multi-threaded
# +: Foreground process group

/proc Filesystem

# View process info in /proc
ls /proc/1234/
# cmdline: Command line arguments
# environ: Environment variables
# fd: Open file descriptors
# maps: Memory maps
# stat: Status information
# status: Detailed status
 
# View command line
cat /proc/1234/cmdline | tr '\0' ' '
 
# View environment
cat /proc/1234/environ | tr '\0' '\n'
 
# View open files
ls -la /proc/1234/fd/
 
# View memory maps
cat /proc/1234/maps
 
# View detailed status
cat /proc/1234/status

Practical Process Management

Monitor System Load

# View load average (1, 5, 15 minute)
uptime
# 10:45:33 up 5 days, 3:12, 2 users, load average: 0.5, 0.6, 0.4
 
# View CPU cores (compare with load)
nproc
# 4 cores
 
# If load > number of cores, system is overloaded
 
# Check CPU usage
top -bn1 | grep "Cpu(s)"
# Cpu(s): 15.2%us, 2.1%sy, 0.0%ni, 82.7%id, 0.0%wa, 0.0%hi, 0.0%si
 
# Monitor specific processes
watch -n 1 'ps aux | grep apache2'
# Update every 1 second

Troubleshoot Zombie Processes

# Find zombie processes
ps aux | grep Z
# USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
# john 1234 0.0 0.0 0 0 ? Z 10:00 0:00 <defunct>
 
# Zombie process parent
ps -o ppid= -p 1234
 
# Kill parent process (cleans up zombie)
kill -9 12345  # Parent PID
 
# Check if zombie still exists
ps aux | grep Z

Manage Resource-Heavy Processes

# Find memory hogs
ps aux --sort=-%mem | head -5
 
# Find CPU hogs
ps aux --sort=-%cpu | head -5
 
# Limit process resources
ulimit -u 100  # Max processes
ulimit -f 100M # Max file size
 
# View limits
ulimit -a
 
# Set nice priority for resource-heavy process
renice 15 -p 1234
 
# Use cgroups for resource control (Ubuntu/Debian)
systemctl --user-unit=process-group.scope \
  --MemoryLimit=500M \
  systemctl start myservice

Best Practices

  1. Regular Monitoring - Use top/htop regularly to catch issues
  2. Graceful Termination - Use SIGTERM before SIGKILL
  3. Background Jobs - Use & for long-running tasks
  4. Priority Management - Adjust nice levels for critical services
  5. Log Monitoring - Check logs for process crashes
  6. Resource Limits - Set appropriate limits to prevent resource exhaustion
  7. Process Trees - Understand parent-child relationships
  8. Cleanup - Kill defunct/zombie processes
  9. Automate - Use systemd for service management
  10. Document - Record why priority was changed

Summary

Process management is critical for system health:

  • ps, top, htop provide process information
  • kill and signals control process behavior
  • Foreground/background jobs enable multitasking
  • nice and renice control process priority
  • Monitoring enables early issue detection
  • Effective management ensures stable, responsive systems

Master these tools for professional Linux administration.