Linux

Process Management on Linux

·2 min read

Everything that runs on a Linux system is a process. The kernel schedules them, the init system supervises them, and you control them through a handful of tools that haven't changed much in thirty years — for good reason.

ps: snapshot the process table

The Unix ps command is the oldest way to list processes. It supports two competing flag styles (BSD and SysV) — most people memorize one incantation:

ps aux              # all processes, user-friendly
ps -ef              # all processes, full format
ps -fp 12345        # specific PID
ps --forest -e      # tree view, every process

The columns you care about: PID, %CPU, %MEM, VSZ (virtual size), RSS (resident set), STAT (state codes — R running, S sleeping, D uninterruptible, Z zombie).

top and htop: live view

top is the BSD-era live monitor and ships everywhere. htop is the modern replacement: colorized, mouse-aware, tree mode with F5, kill via F9. Install it and never look back.

htop                           # interactive
top -b -n 1 | head -20         # batch mode for scripts
top -p $(pgrep -d, nginx)      # only show nginx procs

Signals and kill

kill doesn't necessarily kill — it sends a signal. The kernel delivers signals to processes; what happens next depends on what the process does with them.

kill -TERM 12345      # SIGTERM — "please exit cleanly" (default)
kill -HUP 12345       # SIGHUP — most daemons reload config
kill -KILL 12345      # SIGKILL — uncatchable, the kernel reaps the process
kill -9 12345         # same thing, numeric

pkill -f my-script.py # kill by command-line match
killall nginx         # kill by exact name

SIGKILL is the option of last resort. A process stuck in D state (uninterruptible sleep — usually waiting on disk or NFS) won't even respond to SIGKILL until the syscall returns.

Priority: nice and ionice

The scheduler decides who runs next. nice lets you suggest priority for CPU; values range from -20 (highest) to +19 (lowest). Only root can request negative values.

nice -n 10 ./long-build.sh        # run at lower priority
renice 15 -p 12345                # change priority of running process
ionice -c 3 ./backup.sh            # idle I/O class — yields to anything else

/proc: the kernel's filesystem

Every running process has a directory under /proc/<pid>/. Reading from it is how ps works.

cat /proc/12345/status         # human-readable state
cat /proc/12345/cmdline | tr '\0' ' '  # full command line
ls -l /proc/12345/fd           # all open file descriptors
readlink /proc/12345/exe       # path to the binary
cat /proc/12345/maps           # memory map

When memory runs out: the OOM killer

When the kernel can't allocate memory, it picks a victim and sends SIGKILL. Selection is based on the oom_score in /proc/<pid>/oom_score. You can bias it:

echo -1000 > /proc/$$/oom_score_adj   # never kill this shell
dmesg | grep -i 'killed process'      # see who got reaped
Background vs foreground.

command & runs in background. jobs lists them, fg %1 brings job 1 to foreground, bg %1 resumes a suspended job. disown detaches a job from the shell so it survives logout.

Related