A single runaway process can consume all available CPU or memory and bring your entire VPS to a crawl. This guide covers identifying the culprit and safely stopping it.
Finding the Top CPU Consumers
top
Press Shift+P inside top to sort by CPU (usually the default). Or non-interactively:
ps aux --sort=-%cpu | head -10
Finding the Top Memory Consumers
ps aux --sort=-%mem | head -10
Using htop for a Clearer View
sudo apt install htop -y
htop
Press F6 to change the sort column, F9 to kill a selected process.
Investigating a Specific Process
ps -p PID -o pid,ppid,cmd,%cpu,%mem
Find open files held by a process:
sudo lsof -p PID
Stopping a Process Gracefully
Always try a graceful termination first:
kill PID
This sends SIGTERM, allowing the process to clean up and exit on its own.
Force-Killing an Unresponsive Process
Only if graceful termination fails after a few seconds:
kill -9 PID
SIGKILL terminates immediately without allowing cleanup — use as a last resort, since it can leave temp files or locks behind.
Killing by Process Name
pkill -f process_name
Identifying Zombie Processes
ps aux | grep 'Z'
Zombie processes are already dead but not yet reaped by their parent; they usually clear on their own, but a persistent buildup indicates a bug in the parent process.
Finding What's Using a Specific Port
sudo ss -tulpn | grep :PORT
Preventing Runaway Processes: Resource Limits
For services managed by systemd, you can cap resource usage directly in the unit file:
[Service]
MemoryMax=512M
CPUQuota=50%
sudo systemctl daemon-reload
sudo systemctl restart myapp
Common Errors
"Operation not permitted" when killing a process — the process belongs to another user; use sudo kill PID.
Process restarts immediately after being killed — it's likely managed by systemd with Restart=always; stop it properly with systemctl stop instead.
Best Practices
- Always attempt
kill(SIGTERM) beforekill -9 - Investigate why a process is consuming excess resources rather than just repeatedly killing it
- Set resource limits on services prone to memory leaks
FAQ
What's the difference between kill and kill -9?kill asks the process to terminate gracefully; kill -9 forces immediate termination without cleanup, and should only be used when graceful termination fails.
Related Articles
- How to Check VPS Resource Usage (CPU, RAM & Disk)
- How to Manage Services with systemd and systemctl
- How to Enable and Configure Swap on a Linux VPS
