Linux
Processes
Process management and monitoring in Linux
Processes
Processes are running instances of programs. Understanding process management is crucial for system administration.
Process Basics
Every process has:
- PID (Process ID) - Unique identifier
- PPID (Parent Process ID) - Process that spawned it
- UID (User ID) - Owner of the process
- State - Running, sleeping, zombie, stopped
- Priority - Nice value (-20 to 19)
Viewing Processes
ps Command
# List current user's processes
ps
# List all processes
ps aux # detailed information
ps -ef # different format
# List processes in tree format
ps -ef --forest
# Filter by process name
ps aux | grep nginx
# Show specific columns
ps -o pid,user,cmd -p 1234top Command
# Interactive process monitoring
top
# Non-interactive, update every 2 seconds
top -d 2 -n 5
# Sort by memory usage
top -o %MEM
# Sort by CPU usage
top -o %CPUhtop Command
# Enhanced version of top (requires installation)
htop
# Tree view
htop -t
# Filter by user
htop -u usernamepgrep and pidof
# Find PID by process name
pgrep nginx
pidof nginx
# Get full command line
pgrep -a nginx
# Count processes matching pattern
pgrep -c nginxProcess Control
Starting Processes
# Run process in foreground
./script.sh
# Run process in background
./script.sh &
# Disown process (detach from terminal)
nohup ./long-running-script.sh &
# Use screen or tmux for persistent sessions
screen -S session-name
tmux new-session -s session-nameStopping Processes
# Terminate process gracefully (SIGTERM)
kill PID
kill -15 PID
# Force kill process (SIGKILL)
kill -9 PID
# Kill all processes matching name
killall processname
# Kill processes by pattern
pkill -f "pattern"
# Kill all processes of a user
killall -u usernameSuspending Processes
# Send to background (Ctrl+Z in foreground)
kill -STOP PID
# Resume background process
fg # bring to foreground
bg # continue in background
jobs # list background jobsProcess Priority
Nice Values
# Run with specific priority (-20 highest, 19 lowest)
nice -n 10 ./command
# Change priority of running process
renice 5 -p PID
renice 5 -u username
# Check current nice value
ps -o pid,user,nice,cmdMonitoring Process Resources
# View process memory usage
ps -o pid,user,%mem,rss,vsz,cmd
# Watch a command continuously
watch -n 2 'ps aux | grep nginx'
# Monitor system resources
free -h # memory
df -h # disk
iostat # I/O statisticsBackground Jobs
# List background jobs
jobs
jobs -l # with PID
# Bring job to foreground
fg %1
# Send job to background
bg %1
# Suspend current job
Ctrl+Z
# Disown job (keep running after logout)
disown %1System Processes
Init System (systemd)
# Start/stop services
sudo systemctl start service-name
sudo systemctl stop service-name
sudo systemctl restart service-name
# Enable/disable on boot
sudo systemctl enable service-name
sudo systemctl disable service-name
# Check status
sudo systemctl status service-name
# View logs
sudo journalctl -u service-nameBest Practices
- Use
psandtopto understand system activity - Monitor CPU and memory usage regularly
- Kill processes gracefully first, force kill only when necessary
- Use proper process managers for critical services
- Set appropriate process priorities for important tasks
- Use systemd for service management
- Monitor for zombie processes
- Use tools like supervisor or systemd for process management
- Automate monitoring with monitoring tools