| name | process-management |
| category | system |
| description | Monitor, manage, and control system processes. Use when the user asks to list running processes, find a process by name or PID, kill or terminate processes, manage background jobs, check resource usage, set resource limits, manage system services with systemctl or launchctl, or troubleshoot CPU and RAM usage with ps, top, htop, kill, pgrep, and related tools.
|
| license | MIT |
| metadata | {"author":"zeph","version":"1.0"} |
Process Management
Quick Reference
| Task | Command |
|---|
| List all processes | ps aux |
| Find process by name | pgrep -a name |
| Find process on port | lsof -i :8080 |
| Kill by PID | kill PID |
| Kill by name | pkill name |
| Force kill | kill -9 PID |
| Process tree | pstree |
| Interactive monitor | top or htop |
| Background a command | command & |
| List background jobs | jobs |
Viewing Processes
ps — Process Status
ps aux
ps -ef
ps -ef --forest
ps -u username
ps -p 1234
ps aux | grep nginx
ps -eo pid,ppid,user,%cpu,%mem,vsz,rss,stat,start,time,comm
ps aux --sort=-%cpu | head -20
ps aux --sort=-%mem | head -20
ps -eLf
ps -M PID
ps auxww
ps aux | wc -l
Process Status Codes (STAT column)
| Code | Meaning |
|---|
R | Running or runnable |
S | Sleeping (interruptible) |
D | Uninterruptible sleep (I/O wait) |
T | Stopped (by signal or debugger) |
Z | Zombie (terminated but not reaped) |
I | Idle kernel thread |
< | High priority |
N | Low priority (nice) |
s | Session leader |
l | Multi-threaded |
+ | Foreground process group |
top — Interactive Process Monitor
top
top -u username
top -b -n 1
top -d 2
top -n 1 -b | head -20
top -o cpu
top -o mem
htop — Enhanced Interactive Monitor
htop
htop -u username
htop -t
htop --sort-key=PERCENT_CPU
Key bindings: F5=tree, F6=sort, F9=kill, F4=filter, /=search, u=user filter.
Finding Processes
pgrep — Find Process by Name
pgrep nginx
pgrep -a nginx
pgrep -l nginx
pgrep -x nginx
pgrep -u www-data
pgrep -n nginx
pgrep -o nginx
pgrep -c nginx
pgrep -P 1234
Finding Processes by Resource Usage
ps aux --sort=-%cpu | head -5
ps aux --sort=-%mem | head -5
lsof -i :8080
lsof -i :8080 -sTCP:LISTEN
lsof /path/to/file
lsof -p PID
lsof +D /path/to/dir
pstree — Process Tree
pstree
pstree -p
pstree -p 1234
pstree username
pstree -a
pstree -c
Killing Processes
Signals
| Signal | Number | Action | Use Case |
|---|
SIGHUP | 1 | Hangup | Reload configuration |
SIGINT | 2 | Interrupt | Same as Ctrl+C |
SIGQUIT | 3 | Quit | Quit with core dump |
SIGKILL | 9 | Kill | Force kill (cannot be caught) |
SIGTERM | 15 | Terminate | Graceful shutdown (default) |
SIGSTOP | 19 | Stop | Pause process (cannot be caught) |
SIGCONT | 18 | Continue | Resume stopped process |
SIGUSR1 | 10 | User-defined | Application-specific |
SIGUSR2 | 12 | User-defined | Application-specific |
kill — Send Signal to Process
kill PID
kill -15 PID
kill -TERM PID
kill -9 PID
kill -KILL PID
kill -HUP PID
kill -1 PID
kill -STOP PID
kill -CONT PID
kill PID1 PID2 PID3
kill -- -PGID
pkill — Kill by Name
pkill nginx
pkill -9 nginx
pkill -x nginx
pkill -u username
pkill -f "python.*server.py"
pkill -HUP nginx
killall — Kill All by Name
killall nginx
killall -9 nginx
killall -u username
killall -w nginx
Graceful Shutdown Pattern
kill PID && sleep 5 && kill -0 PID 2>/dev/null && kill -9 PID
Job Control
Background and Foreground
command &
jobs
jobs -l
fg
fg %1
bg
bg %2
nohup command &
nohup command > output.log 2>&1 &
command &
disown %1
disown -h %1
wait
wait PID
wait %1
Job Identifiers
| Identifier | Meaning |
|---|
%1 | Job number 1 |
%+ or %% | Current (most recent) job |
%- | Previous job |
%string | Job whose command starts with string |
%?string | Job whose command contains string |
Resource Limits
ulimit — User Limits
ulimit -a
ulimit -n
ulimit -n 65536
ulimit -u
ulimit -u 4096
ulimit -s
ulimit -v
ulimit -f
ulimit -c
ulimit -c unlimited
ulimit -Hn
nice and renice — Process Priority
nice -n 10 command
sudo nice -n -10 command
renice 10 -p PID
renice 5 -u username
Service Management
systemctl (Linux, systemd)
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
systemctl status nginx
sudo systemctl enable nginx
sudo systemctl disable nginx
systemctl list-units --type=service
systemctl list-units --type=service --state=running
systemctl list-units --type=service --state=failed
journalctl -u nginx
journalctl -u nginx -f
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx -n 50
systemctl is-active nginx
systemctl is-enabled nginx
sudo systemctl mask nginx
sudo systemctl unmask nginx
launchctl (macOS)
launchctl list
launchctl list | grep nginx
sudo launchctl load /Library/LaunchDaemons/com.example.service.plist
sudo launchctl unload /Library/LaunchDaemons/com.example.service.plist
launchctl load ~/Library/LaunchAgents/com.example.agent.plist
launchctl unload ~/Library/LaunchAgents/com.example.agent.plist
launchctl bootstrap system /Library/LaunchDaemons/com.example.plist
launchctl bootout system /Library/LaunchDaemons/com.example.plist
launchctl print system/com.example.service
sudo launchctl kickstart system/com.example.service
sudo launchctl kickstart -k system/com.example.service
Common Workflows
Find and Kill Process on Port
lsof -i :8080
kill $(lsof -t -i :8080)
kill -9 $(lsof -t -i :8080)
Find Zombie Processes
ps aux | awk '$8 ~ /Z/'
ps -o ppid= -p ZOMBIE_PID
Important Notes
- Always try
SIGTERM before SIGKILL (-9); SIGKILL does not allow cleanup
kill -0 PID checks if a process exists without sending a signal
- Zombie processes cannot be killed; kill their parent to reap them
- On macOS,
ps aux works but some Linux-specific flags (like --forest) are unavailable
ulimit changes apply only to the current shell session and its children