Skip to content
Foundations · 14 min

Linux in one sitting: processes, memory, files

Almost every troubleshooting question ends up at one of three things: a process, memory, or a file. This lesson gives you a working model of each and the handful of commands that let you look at them.

Processes

A process is a running program with its own memory, a numeric ID (the PID), a parent, and a table of open files. A new process is created when an existing one calls fork, which copies it, and usually then exec, which replaces the copy with a different program. When a process ends, its parent must collect the exit status; until it does, the dead child stays in the process table as a zombie. Zombies use no memory or CPU, but each one holds a PID, and a parent that never collects them will eventually exhaust the PID limit.

Processes are controlled with signals. SIGTERM asks a process to shut down and can be handled; SIGKILL cannot be caught and gives the process no chance to clean up. In an interview, reach for SIGTERM first and say why.

$ ps -eo pid,ppid,stat,etime,cmd --sort=-etime | head   # what is running, and for how long
$ ps -eo stat | grep -c '^Z'                              # how many zombies
$ cat /proc/sys/kernel/pid_max; ulimit -u                 # system and per-user limits

Memory

Linux uses spare memory to cache file contents, so "free" memory is normally small on a healthy machine. The number that matters is available in free -m: how much could be handed to programs without swapping. When memory truly runs out, the kernel's OOM killer picks a process and kills it, and records the decision in the kernel log.

$ free -m                              # read the "available" column, not "free"
$ dmesg -T | grep -i 'killed process'  # did the OOM killer act, and on what
$ ps -eo pid,rss,cmd --sort=-rss | head  # who is holding the most memory
Containers change the questionA container can be killed for exceeding its own memory limit while the machine has plenty free. Always ask whether the limit that was hit belongs to the host or to the container.

Files and disks

A filename is a pointer to an inode, and the inode points to the data blocks. A filesystem can run out of blocks (bytes) or out of inodes (file count), and either one stops writes. A deleted file keeps its blocks for as long as some process still has it open, which is why df and du can disagree. Each process also has a limit on open file descriptors; sockets count against it too.

$ df -h; df -i                  # bytes, then inodes
$ lsof +L1                      # deleted files that are still open
$ ls /proc/<pid>/fd | wc -l     # descriptors in use by one process
$ cat /proc/<pid>/limits        # that process's actual limits
Linked exercise · needs a computer · 14 minDisk full on a log-heavy nodeRead it here, do it on a computer

Load is not CPU

Load average counts processes that are running or waiting to run, and on Linux also those blocked on disk I/O. A machine can show a load of 40 with idle CPUs if dozens of processes are stuck waiting on a slow disk or a hung network mount. Compare the load with the number of CPUs (nproc), then look at vmstat 1: the r column is CPU demand, b is blocked on I/O, and wa is the share of time spent waiting for it.

Ten commands to know cold

  • uptime: load averages over 1, 5 and 15 minutes; rising or falling?
  • dmesg -T | tail: what the kernel complained about most recently.
  • vmstat 1: run queue, blocked processes, swap, I/O wait.
  • free -m: available memory.
  • df -h and df -i: disk bytes and inodes.
  • ps aux --sort=-%cpu | head: who is using the CPU.
  • ss -tlnp: which processes are listening on which ports.
  • journalctl -u --since '10 min ago': recent logs for one service.
  • strace -p -f -tt: what system calls a process is making right now. It slows the process, so say so before using it in production.
  • lsof -p : every file and socket a process has open.
Interview tipDo not recite commands. Say what you want to learn, then the command, then what each result would mean: "I want to know if we are out of memory or out of CPU, so I would run vmstat; a high r column points at CPU, a high wa column points at disk."