patternbashModerate
Process Management: ps, top, htop, and Signals
Viewed 0 times
pskillsigtermsigkillpgreppkillzombieprocess treehtop
linux
Error Messages
Problem
Finding and managing processes — identifying CPU/memory hogs, killing stuck processes, and understanding process trees — requires different tools for different situations.
Solution
Use ps for scripting, top/htop for interactive monitoring, and kill with specific signals.
# Find processes by name
ps aux | grep nginx
pgrep -la nginx
# Process tree
pstree -p
ps axjf
# Top CPU consumers
ps aux --sort=-%cpu | head -15
# Top memory consumers
ps aux --sort=-%mem | head -15
# Kill process gracefully (SIGTERM)
kill 1234
# Kill forcefully (SIGKILL) — last resort
kill -9 1234
# Kill all processes matching a name
pkill -f 'python manage.py'
# Send signal to process group
kill -TERM -$(pgrep -f myapp)
# Wait for a process to finish
wait 1234Why
SIGTERM (15) asks the process to clean up and exit gracefully. SIGKILL (9) cannot be caught or ignored — it terminates immediately but may leave temp files or locks behind. Always try SIGTERM first.
Gotchas
- kill -9 is not a first resort — it skips cleanup handlers and can leave resources in a dirty state.
ps auxon Linux shows all users' processes; BSD-style flags differ from POSIX flags.- Zombie processes (Z state in ps) are already dead — kill their parent to reap them.
- htop requires ncurses and may not be installed by default on minimal systems.
Revisions (0)
No revisions yet.