Overview and What You Will Learn
A Linux process is a running instance of a program, with its own PID, owner, memory and open files. Process management means finding the right process, reading its state and resource use, and stopping or reprioritising it with the right signal.
By the end of this lab you will be able to:
- Read
ps auxandtopoutput and know which columns to trust - Explain the process states
R,S,D,TandZand what each means during an incident - Send the correct signal (SIGTERM, SIGHUP, SIGKILL) for each situation
- Lower the priority of heavy jobs with
nice,reniceandionice - Run long jobs that survive a closed terminal
- Read live process details from
/proc
Why This Matters in Production
Process problems usually appear as one of two incidents: a runaway process that consumes CPU or memory until the server stops responding, or processes that pile up in a bad state such as zombies or uninterruptible sleep. Consider a trading platform at market open when memory spikes. An engineer who knows ps can rank processes by memory in seconds and see which one is responsible, instead of guessing for twenty minutes. The same skills tell you whether the right fix is a signal, a restart through systemd or a deeper look at disk I/O.
Core Principles
Every process is created by another one: a parent calls fork() to copy itself, and the child usually calls exec() to run a new program. PID 1 (systemd) is the ancestor of everything, and it adopts orphaned processes. A process then moves between a handful of states, and the state shown in ps tells you what it is doing right now.
What each state means:
R(running or runnable) is on a CPU or waiting for oneS(interruptible sleep) is waiting for an event and can receive signalsD(uninterruptible sleep) is waiting on I/O inside the kernel, such as a disk or a network filesystem. Signals, including SIGKILL, are delayed until the call returns, which is why these processes seem unkillableT(stopped) was paused by SIGSTOP or Ctrl+ZZ(zombie) has exited but its parent has not collected the exit status. A zombie holds no memory or CPU, only a slot in the process table
The ps aux columns that matter most:
USER PID %CPU %MEM VSZ RSS STAT START TIME COMMANDroot 1 0.0 0.1 172036 13312 Ss Oct01 0:05 /sbin/initwww-data 892 1.2 0.8 524288 81920 S 10:00 0:12 nginx: worker processpayment 1350 45.3 2.1 1258291 215040 R 10:05 1:23 node index.jsroot 1401 0.0 0.0 0 0 Z 10:10 0:00 [cleanup] <defunct>VSZis the virtual address space claimed, in KiB. It includes memory that is mapped but never loadedRSSis the physical RAM in use, in KiB, and is the number to trustSTATis the state letter plus flags (ssession leader,<high priority,Nlow priority,lmulti-threaded,+foreground)TIMEis total CPU time used, not wall-clock time
Remember
%CPUinpsis the average over the whole life of the process (CPU time divided by elapsed time), not current usage. A process that was busy for an hour and is now idle still shows a high number. Usetoporpidstat 1to see what a process is doing now.
Detailed Step-by-Step Practical Lab
How to List and Sort Running Processes with ps
ps takes a snapshot of processes. Use ps aux for a full list, --sort to rank by CPU or memory, and pgrep or ps -C to find a specific process without matching the search command itself.
ps aux # All processes, UNIX style (shows PPID)ps -ef # Tree view: who started whomps -ef --forest # Top 10 by memory (RSS) and by lifetime-average CPUps aux --sort=-%mem | head -10ps aux --sort=-%cpu | head -10 # Find a process without matching grep itselfpgrep -la nginxps -C nginx -o pid,ppid,user,stat,%mem,cmd # Selected fields for one PIDps -p 1350 -o pid,ppid,user,ni,stat,rss,etime,cmd # Threads of a processps -L -p 1350 -o pid,tid,pcpu,stat,comm # Find zombies and uninterruptible (D state) processesps -eo pid,ppid,stat,wchan:32,comm | awk '$3 ~ /^[ZD]/'How to Monitor Processes in Real Time with top and htop
top refreshes a live view of CPU, memory and processes. The summary at the top gives the system picture and the table below ranks processes, so read the header first and then sort the table.
topUseful keys inside top:
Psorts by CPU andMsorts by memory1toggles one line per CPU coreHswitches between processes and threadskkills a process (it prompts for the PID and signal) andrrenices oneqquits
top - 10:23:45 up 15 days, load average: 1.23, 0.87, 0.65Tasks: 245 total, 2 running, 243 sleeping, 0 stopped, 1 zombie%Cpu(s): 12.3 us, 2.1 sy, 0.0 ni, 84.1 id, 1.2 wa, 0.0 hi, 0.3 si, 0.0 stHow to read the header:
Taskscounts processes by state. A growingzombiecount means a parent is not reaping its childrenusandsyare user and kernel CPU time, andidis idlewais time waiting for I/O. A persistently high value points at storage, so confirm withiostat -xstis CPU stolen by the hypervisor on a VM
# Non-interactive snapshot for scripts and ticketstop -b -n 1 | head -20 # Watch one process, or sort by memory from the starttop -p 1350top -o %MEM # htop: friendlier view with colour, mouse and taggingsudo apt install htophtopIn htop, press F6 to choose a sort column, F5 for a tree view, F9 to send a signal and Space to tag several processes for one action.
How to Send Signals with kill, pkill and killall
A signal is a message sent to a process. kill does not necessarily kill: it delivers a signal, and what happens next depends on the signal and on whether the program handles it. Use signal names rather than numbers, because some numbers differ between architectures.
| Signal | Typical use | Can the process catch it? |
|---|---|---|
SIGTERM (15) |
Polite request to shut down | Yes |
SIGHUP (1) |
Reload configuration, if the program supports it | Yes |
SIGINT (2) |
Same as Ctrl+C | Yes |
SIGKILL (9) |
Immediate forced termination | No |
SIGSTOP |
Pause a process | No |
SIGCONT |
Resume a paused process | Yes |
# List signal nameskill -l # Polite shutdown (SIGTERM is the default)kill 1350kill -TERM 1350 # Wait, then check whether it is still alivesleep 15kill -0 1350 && echo "still running" # Force kill: last resort, no cleanupkill -KILL 1350 # Reload config (only for programs that handle SIGHUP, such as nginx)kill -HUP 1350 # Pause and resumekill -STOP 1350kill -CONT 1350 # By name: check what will match firstpgrep -la 'node worker'pkill -f 'node worker'kill -0 sends no signal. It only reports whether the process exists and you are allowed to signal it, so it is a safe liveness check.
Common MistakeReaching for
kill -9first. SIGKILL gives the program no chance to flush buffers, close database connections or remove lock files. Send SIGTERM, wait 10 to 30 seconds, and use SIGKILL only if it does not exit.
Common MistakeSending SIGHUP to reload a program that does not handle it. By default SIGHUP terminates a process, so a Node.js app without a handler will simply exit. Check the program's documentation first, or use
systemctl reload.
TipFor services managed by systemd, stop them with
sudo systemctl stop namerather thankill. If you kill the process directly,Restart=on-failuremay start it again, and systemd keeps accurate state.
How to Change Process Priority with nice, renice and ionice
Niceness ranges from -20 (highest priority) to 19 (lowest), and the default is 0. It only matters when the CPU is contended, and only root can lower the value. Use it to stop background jobs such as backups from slowing the main service.
# Start a backup at the lowest CPU and idle I/O prioritynice -n 19 ionice -c 3 pg_dump -U postgres payments > /backup/payments.sql # Lower the priority of a running processrenice -n 10 -p 1350 # Raise priority (root only)sudo renice -n -5 -p 892 # Check nicenessps -o pid,ni,cmd -p 1350How to Run Background Jobs That Survive Logout
Shell job control (&, fg, bg) is for interactive work, and nohup only protects against the terminal closing. For anything that must keep running, hand it to systemd.
# Run in the background of this shell./long-running-script.sh &jobs # Bring it to the foreground, or pause it with Ctrl+Z and resume in the backgroundfg %1bg %1 # Immune to SIGHUP when the terminal closesnohup ./long-running-script.sh > /tmp/script.log 2>&1 & # Detach an already-running job from this shelldisown -h %1 # Wait for a specific PID or for all background jobswait 2847wait # Better for real work: run it as a transient systemd service with logs in the journalsudo systemd-run --unit=db-backup /opt/backup/run-backup.shjournalctl -u db-backup -fHow to Read Process Details from /proc
Every process has a directory /proc/PID/ that exposes its live state straight from the kernel. It is the quickest way to see a process's command line, open files, limits and memory without extra tools.
# Full command line (arguments are separated by NUL bytes)tr '\0' ' ' < /proc/1350/cmdline # Where it runs from and which binary it isls -l /proc/1350/cwd /proc/1350/exe # Open file descriptors versus the limitls /proc/1350/fd | wc -lgrep 'open files' /proc/1350/limits # Memory and threads (values are in kB)grep -E 'VmRSS|VmSize|Threads' /proc/1350/status# VmRSS: 215040 kB# VmSize: 1258291 kB# Threads: 8 # Watch memory over time to spot a leakwatch -n 5 "grep VmRSS /proc/1350/status" # Where is a D-state process stuck? (root only)sudo cat /proc/1350/stackSecurity
/proc/PID/environcan contain database passwords and API keys passed as environment variables. It is readable only by the process owner and root, so do not paste its output into tickets or chat.
How to Deal with Zombie and D-State Processes
Zombies and D-state processes cannot be removed with kill -9, so the fix is elsewhere.
- A zombie has already exited, so there is nothing to kill. Find its parent with
ps -o ppid= -p ZOMBIE_PID, fix the parent so it callswait(), or restart the parent. When the parent exits, PID 1 adopts and reaps the zombies - A
D-state process is waiting on the kernel, usually for a slow or failed disk or network mount. Find the cause withiostat -x,dmesg -Tandmount, and fix the storage problem. The process finishes when the I/O does - Zombies are only dangerous in large numbers, because they use up PID table entries until new processes cannot be created. A few are harmless
Production Best Practices & Common Pitfalls
- Signal gently first. SIGTERM, a pause, then SIGKILL gives programs a chance to clean up.
- Use systemd for managed services. It tracks state and restart policy, while a raw
killfights it. - Verify before you pkill. Run
pgrep -la patternto see exactly what will match. - Judge by current numbers. Use
top,pidstat 1and/procinstead of lifetime-average%CPU. - Rank by RSS. It reflects real memory use, while VSZ overstates it.
- Deprioritise batch work. Run backups and reports under
niceandionice.
| Scenario | Wrong approach | Correct approach |
|---|---|---|
| Service not responding | kill -9 immediately |
kill -TERM, wait, then kill -9 |
| High CPU process | Reboot the server | top, identify it, then find the root cause |
| Zombie processes | Try to kill the zombie | Fix or restart the parent |
| D-state process | kill -9 |
Fix the storage or network problem |
| Memory keeps growing | Restart and move on | Track VmRSS over time to confirm a leak |
| Long job over SSH | Plain & |
systemd-run, or tmux |
Quick Reference & Troubleshooting Commands
| Task | Command |
|---|---|
| All processes | ps aux |
| Process tree | ps -ef --forest |
| Find a process | pgrep -la name |
| Top by memory | ps aux --sort=-%mem |
| Live view | top or htop |
| One-shot top snapshot | top -b -n 1 |
| Graceful stop | kill -TERM PID |
| Force kill | kill -KILL PID |
| Reload config | kill -HUP PID |
| Is it alive? | kill -0 PID |
| Lower priority | renice -n 10 -p PID |
| Open file count | ls /proc/PID/fd |
| Memory of a process | grep VmRSS /proc/PID/status |
| Detached job | sudo systemd-run --unit=job /path/script.sh |
| Symptom | First command | What to look for |
|---|---|---|
| Server slow, one process hot | top then P |
One PID using most CPU now |
| Memory keeps rising | ps aux --sort=-%mem |
A single process with growing RSS |
| Cannot fork new processes | ps -eo stat |
Thousands of Z entries |
| Process will not die | ps -o pid,stat,wchan -p PID |
State D, then check storage |
| Signal ignored | grep Sig /proc/PID/status |
Signal appears in the ignored or blocked masks |