Skip to main content

Managing Linux Processes with ps, top and Signals

Inspect and control Linux processes with ps, top, htop, kill and signals, and read process states and /proc to fix production incidents.

30 Terms

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 aux and top output and know which columns to trust
  • Explain the process states R, S, D, T and Z and what each means during an incident
  • Send the correct signal (SIGTERM, SIGHUP, SIGKILL) for each situation
  • Lower the priority of heavy jobs with nice, renice and ionice
  • 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.

stateDiagram-v2 [*] --> R R --> S: waits for event S --> R: event arrives R --> D: blocking I/O D --> R: I/O completes R --> T: SIGSTOP T --> R: SIGCONT R --> Z: process exits Z --> [*]: parent reaps

What each state means:

  • R (running or runnable) is on a CPU or waiting for one
  • S (interruptible sleep) is waiting for an event and can receive signals
  • D (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 unkillable
  • T (stopped) was paused by SIGSTOP or Ctrl+Z
  • Z (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:

TEXT
USER PID %CPU %MEM VSZ RSS STAT START TIME COMMAND
root 1 0.0 0.1 172036 13312 Ss Oct01 0:05 /sbin/init
www-data 892 1.2 0.8 524288 81920 S 10:00 0:12 nginx: worker process
payment 1350 45.3 2.1 1258291 215040 R 10:05 1:23 node index.js
root 1401 0.0 0.0 0 0 Z 10:10 0:00 [cleanup] <defunct>
  • VSZ is the virtual address space claimed, in KiB. It includes memory that is mapped but never loaded
  • RSS is the physical RAM in use, in KiB, and is the number to trust
  • STAT is the state letter plus flags (s session leader, < high priority, N low priority, l multi-threaded, + foreground)
  • TIME is total CPU time used, not wall-clock time
Remember

%CPU in ps is 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. Use top or pidstat 1 to 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.

Bash
ps aux
# All processes, UNIX style (shows PPID)
ps -ef
# Tree view: who started whom
ps -ef --forest
# Top 10 by memory (RSS) and by lifetime-average CPU
ps aux --sort=-%mem | head -10
ps aux --sort=-%cpu | head -10
# Find a process without matching grep itself
pgrep -la nginx
ps -C nginx -o pid,ppid,user,stat,%mem,cmd
# Selected fields for one PID
ps -p 1350 -o pid,ppid,user,ni,stat,rss,etime,cmd
# Threads of a process
ps -L -p 1350 -o pid,tid,pcpu,stat,comm
# Find zombies and uninterruptible (D state) processes
ps -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.

Bash
top

Useful keys inside top:

  • P sorts by CPU and M sorts by memory
  • 1 toggles one line per CPU core
  • H switches between processes and threads
  • k kills a process (it prompts for the PID and signal) and r renices one
  • q quits
TEXT
top - 10:23:45 up 15 days, load average: 1.23, 0.87, 0.65
Tasks: 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 st

How to read the header:

  • Tasks counts processes by state. A growing zombie count means a parent is not reaping its children
  • us and sy are user and kernel CPU time, and id is idle
  • wa is time waiting for I/O. A persistently high value points at storage, so confirm with iostat -x
  • st is CPU stolen by the hypervisor on a VM
Bash
# Non-interactive snapshot for scripts and tickets
top -b -n 1 | head -20
# Watch one process, or sort by memory from the start
top -p 1350
top -o %MEM
# htop: friendlier view with colour, mouse and tagging
sudo apt install htop
htop

In 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
Bash
# List signal names
kill -l
# Polite shutdown (SIGTERM is the default)
kill 1350
kill -TERM 1350
# Wait, then check whether it is still alive
sleep 15
kill -0 1350 && echo "still running"
# Force kill: last resort, no cleanup
kill -KILL 1350
# Reload config (only for programs that handle SIGHUP, such as nginx)
kill -HUP 1350
# Pause and resume
kill -STOP 1350
kill -CONT 1350
# By name: check what will match first
pgrep -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 Mistake

Reaching for kill -9 first. 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 Mistake

Sending 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.

Tip

For services managed by systemd, stop them with sudo systemctl stop name rather than kill. If you kill the process directly, Restart=on-failure may 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.

Bash
# Start a backup at the lowest CPU and idle I/O priority
nice -n 19 ionice -c 3 pg_dump -U postgres payments > /backup/payments.sql
# Lower the priority of a running process
renice -n 10 -p 1350
# Raise priority (root only)
sudo renice -n -5 -p 892
# Check niceness
ps -o pid,ni,cmd -p 1350
How 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.

Bash
# 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 background
fg %1
bg %1
# Immune to SIGHUP when the terminal closes
nohup ./long-running-script.sh > /tmp/script.log 2>&1 &
# Detach an already-running job from this shell
disown -h %1
# Wait for a specific PID or for all background jobs
wait 2847
wait
# Better for real work: run it as a transient systemd service with logs in the journal
sudo systemd-run --unit=db-backup /opt/backup/run-backup.sh
journalctl -u db-backup -f
How 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.

Bash
# Full command line (arguments are separated by NUL bytes)
tr '\0' ' ' < /proc/1350/cmdline
# Where it runs from and which binary it is
ls -l /proc/1350/cwd /proc/1350/exe
# Open file descriptors versus the limit
ls /proc/1350/fd | wc -l
grep '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 leak
watch -n 5 "grep VmRSS /proc/1350/status"
# Where is a D-state process stuck? (root only)
sudo cat /proc/1350/stack
Security

/proc/PID/environ can 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 calls wait(), 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 with iostat -x, dmesg -T and mount, 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 kill fights it.
  • Verify before you pkill. Run pgrep -la pattern to see exactly what will match.
  • Judge by current numbers. Use top, pidstat 1 and /proc instead of lifetime-average %CPU.
  • Rank by RSS. It reflects real memory use, while VSZ overstates it.
  • Deprioritise batch work. Run backups and reports under nice and ionice.
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

Resources

Explore More in Linux Process and System Management

All 6 Topics

Frequently Asked Questions

Is Managing Linux Processes with ps, top and Signals free to learn on DevOps Network?

Yes - this topic, like everything on DevOps Network, is 100% free with no paywall or sign-up gate.

What does the Managing Linux Processes with ps, top and Signals topic cover?

Inspect and control Linux processes with ps, top, htop, kill and signals, and read process states and /proc to fix production incidents.

Found a mistake, an outdated step or a broken link?