HxHippy

Process Management Essentials

Control, monitor, and troubleshoot Linux processes effectively.

Last updated: 2024-12-18

Learn to view, control, and troubleshoot processes - the running instances of programs on your system.

Understanding Processes

Every process has:

  • PID: Unique process ID
  • PPID: Parent process ID
  • UID: User running the process
  • State: Running, sleeping, stopped, zombie
  • Priority: Scheduling priority (nice value)

Viewing Processes

ps - Process Snapshot

# Your processes
ps

# All processes (BSD style)
ps aux

# All processes (Unix style)
ps -ef

# Process tree
ps axjf
# or
pstree

# Specific user's processes
ps -u username

# Find process by name
ps aux | grep nginx
# Better: pgrep
pgrep -a nginx

Understanding ps aux Output

USER   PID %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMAND
root     1  0.0  0.1 169936 11924 ?    Ss   Dec17   0:02 /sbin/init

- USER: Process owner
- PID: Process ID
- %CPU: CPU usage percentage
- %MEM: Memory usage percentage
- VSZ: Virtual memory size (KB)
- RSS: Resident set size (actual RAM used)
- TTY: Terminal (? = no terminal)
- STAT: Process state
- START: When process started
- TIME: CPU time used
- COMMAND: Command that started it

Process States (STAT)

Code State
R Running
S Sleeping (interruptible)
D Disk sleep (uninterruptible)
Z Zombie (terminated but not reaped)
T Stopped
s Session leader
+ Foreground process
l Multi-threaded
< High priority
N Low priority

top - Real-time Monitoring

# Start top
top

# Interactive commands in top:
# h - Help
# q - Quit
# k - Kill process (enter PID)
# r - Renice process
# M - Sort by memory
# P - Sort by CPU
# 1 - Show individual CPUs
# c - Show full command
# u - Filter by user

# Better alternative: htop
htop

Controlling Processes

Signals

# Common signals
# SIGTERM (15) - Graceful termination (default)
# SIGKILL (9)  - Force kill (cannot be caught)
# SIGHUP (1)   - Hangup (often used to reload config)
# SIGSTOP (19) - Pause process
# SIGCONT (18) - Resume process
# SIGINT (2)   - Interrupt (Ctrl+C)

# Kill by PID
kill 1234           # SIGTERM
kill -9 1234        # SIGKILL
kill -TERM 1234     # Same as default

# Kill by name
killall nginx       # All processes named nginx
pkill nginx         # Pattern match
pkill -u user       # All user's processes

# Reload configuration (common pattern)
kill -HUP $(cat /var/run/nginx.pid)
# or
killall -HUP nginx

Background and Foreground Jobs

# Run in background
./long_task.sh &

# Run in background, ignore hangup
nohup ./long_task.sh &

# View background jobs
jobs

# Bring to foreground
fg %1

# Send to background
# First: Ctrl+Z (suspend)
bg %1

# Disown a job (keeps running after logout)
disown %1

Process Priority (nice)

# Nice values: -20 (highest priority) to 19 (lowest)
# Default: 0, only root can set negative values

# Start with low priority
nice -n 10 ./cpu_intensive_task

# Start with high priority (root only)
sudo nice -n -10 ./important_task

# Change priority of running process
renice 5 -p 1234
sudo renice -10 -p 1234

# See nice values
ps -o pid,ni,comm

Process Information

Detailed Process Info

# Process details from /proc
cat /proc/1234/status
cat /proc/1234/cmdline
cat /proc/1234/environ
ls -la /proc/1234/fd  # Open file descriptors
ls -la /proc/1234/cwd  # Current working directory

# Open files by process
lsof -p 1234

# Files opened by all nginx processes
lsof -c nginx

# Who's using a file
lsof /var/log/syslog

# Who's using a port
lsof -i :80

Memory and CPU

# Process memory map
pmap 1234

# CPU affinity (which CPUs can run the process)
taskset -p 1234

# Set CPU affinity
taskset -c 0,1 ./process  # Run on CPUs 0 and 1

Troubleshooting

Find Resource Hogs

# Top CPU consumers
ps aux --sort=-%cpu | head -10

# Top memory consumers
ps aux --sort=-%mem | head -10

# Processes in D (uninterruptible sleep) state
ps aux | awk '$8 ~ /D/'

Handle Zombie Processes

# Find zombies
ps aux | grep 'Z'

# Zombies are reaped when parent exits or calls wait()
# Kill the parent process to clean up
kill $(ps -o ppid= -p ZOMBIE_PID)

Track Process

# Watch process resource usage
watch -n 1 'ps -p 1234 -o %cpu,%mem,etime'

# System calls made by process
strace -p 1234

# New process system calls
strace ./program

# Library calls
ltrace ./program

Practical Scripts

Kill All User Processes

#!/bin/bash
# Kill all processes for a user (careful!)
pkill -u "$1"

Monitor Process

#!/bin/bash
# Alert if process not running
PROC="nginx"
if ! pgrep -x "$PROC" > /dev/null; then
    echo "$PROC is not running!" | mail -s "Alert" [email protected]
fi

Resource Limits

# View limits
ulimit -a

# Set max open files for session
ulimit -n 65535

# Persistent limits (in /etc/security/limits.conf)
# username soft nofile 65535
# username hard nofile 65535
intermediate System Administration 30 min read

Related Tutorials

pstopkillprocessessignalsbackground jobs