Managing Processes with ps, top, htop, kill, and nice
Identify runaway processes, understand their resource usage, and manage them cleanly using core Linux process tools.
Netbay Engineering
Netbay Engineering
On this page
Every running program on a Linux system is a process. When your web server consumes unexpected memory, a background job hangs, or a deployment script spawns too many workers, you need to identify the problem process, understand what resources it is consuming, and take action. The core tools for this are ps, top, htop, kill, and nice.
ps: Snapshot of Running Processes
The ps command produces a point-in-time snapshot of all processes. The most useful form for interactive diagnosis is:
ps aux --sort=-%mem | head -20
ps -eo pid,ppid,user,%cpu,%mem,comm --sort=-%cpu | head -15The first command lists all processes sorted by memory usage in descending order and shows the top 20 consumers. The second uses custom output columns sorted by CPU usage. The --sort flag is far more reliable than piping through sort because ps reads the process table atomically.
To find a specific process by name:
ps aux | grep nginx
ps -C nginx -o pid,ppid,%cpu,%mem,cmdThe -C flag matches by command name without needing grep, which avoids the common problem of grep matching itself in the output.
top and htop: Real-Time Monitoring
top provides a live, updating view of system processes:
top
top -u www-dataPress M to sort by memory, P to sort by CPU, and k to kill a process by PID. The load average in the top header shows 1-minute, 5-minute, and 15-minute averages. A load average higher than your CPU count means the system is overloaded.
htop is a more user-friendly alternative with color output, mouse support, and a tree view. Most distributions require installing it:
apt install htop
dnf install htopPress F5 for tree view, F6 to select a sort column, and F9 to send signals to processes. Tree view is particularly useful for understanding parent-child relationships, such as which bash shell spawned a runaway script.
Killing Processes: Signals and Exit Codes
The kill command sends signals to processes. The most common signals are:
- **SIGTERM (15)**: Graceful shutdown. The process can clean up and exit. This is the default signal.
- **SIGKILL (9)**: Forceful termination. The kernel immediately removes the process. Use this as a last resort because the process cannot clean up open files or temporary data.
- **SIGHUP (1)**: Reload configuration. Many daemons like nginx and sshd re-read their config files on SIGHUP.
kill 12345
kill -9 12345
kill -HUP $(cat /var/run/nginx.pid)
killall -u www-dataAlways try SIGTERM first. Only escalate to SIGKILL if the process does not respond within a few seconds. For a running script in your terminal, Ctrl+C sends SIGINT, and Ctrl+Z sends SIGTSTP to pause it.
nice: Controlling Process Priority
The nice value controls scheduling priority. Values range from -20 (highest priority) to 19 (lowest priority). A default process starts at nice 0. Running a CPU-intensive task with low priority prevents it from starving other processes:
nice -n 10 tar czf backup.tar.gz /var/www/
nice -n 19 ./heavy-computation.shTo change the priority of an already running process, use renice:
renice -n 15 -p 12345This is especially useful on VPS instances where a single heavy process can saturate the CPU and cause your web server or database to become unresponsive.
Takeaway
Process management is a daily task on any Linux VPS. Learn the signal hierarchy, use ps and htop to diagnose resource hogs, and set priorities with nice to keep critical services responsive. Spin up a Netbay VPS and practice these tools hands-on at netbayhosts.in.
Keep reading
Follow along on a real VPS
Deploy Linux in under 60 seconds
These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.
Deploy an instance