KatInterview

Diagnose and Remediate a Runaway Process on Linux

System DesignModel answer
MediumModel answerSystem Designlinuxcliprocess-management

Last updated

Given a degraded production host, use top, ps, find, and kill to identify a CPU-hungry process, handle files with hostile names, and terminate it safely.

Scenario

A production server has triggered high-CPU alerts and the application partition is nearly full. You have an SSH session. No other team member is available. Your job is to investigate the problem, handle the log files that are eating disk space, and stop the offending process — all without disrupting healthy workloads running alongside it.

Walk through the following three phases, providing exact commands with flags and a rationale for each choice.


Phase 1 — Identify the Offending Process

  • Use top in a mode that is safe to pipe or log (non-interactive / batch mode), and explain how to get a meaningful CPU reading rather than a noisy first-sample artifact.
  • Follow up with a ps snapshot that captures, for a specific PID: the parent PID, full untruncated command-line arguments, thread count, and absolute start time.
  • Explain what each of those fields tells you operationally (for example, why the parent PID matters before you decide to kill).

Phase 2 — Locate and Handle Large Scratch Files

The process writes temporary logs to /var/log/app_scratch/. Filenames may include spaces, leading dashes, newlines, or other non-printable characters.

  • Write a find command that returns only regular files on that filesystem, larger than 500 MB, last modified within the past 60 minutes.
  • Show how to safely consume those results in a downstream command (disk-usage summary or archive) without breaking on any of those filename edge cases.
  • Call out the classic trap: what happens to disk space if the process still has one of those files open when you delete it, and how do you recover?

Phase 3 — Stop the Process Without Making Things Worse

  • Describe the step-by-step sequence to terminate the process, starting with the gentler option.
  • Contrast SIGTERM and SIGKILL: what can each signal do and not do, and why does jumping straight to kill -9 turn a CPU incident into a potential data-integrity incident for stateful or I/O-heavy services?
  • Show how to poll for exit without false positives (zombies, EPERM), re-verify the process's identity before escalating, and explain why PID alone is not a sufficient identifier.

What a Complete Answer Looks Like

For each phase, provide runnable bash commands (with flags explained), the reasoning behind your choices, and the failure modes you are actively avoiding. Signal semantics and safe filename handling should be explained in prose, not just implied by the code.

Hints

Hint 1

For a reliable CPU reading from top, one iteration samples over too short a window to be meaningful — run two iterations with a delay and read the second block. Batch mode (-b) makes the output pipeable without curses escape codes.

Hint 2

NUL (\0) is the only byte that cannot appear in a POSIX filepath. -print0 paired with xargs -0, du --files0-from=-, or tar --null is the only fully general way to pass arbitrary filenames between commands. Never use for f in $(find ...) or bare xargs without -0.

Hint 3

Before sending any signal, record the process's start time alongside its PID — the pair (PID, start time) is the true identity. After SIGTERM, poll with kill -0 in a timed loop rather than sleeping a fixed amount; but watch out: kill -0 succeeds on zombies, and fails with EPERM on a live process you don't own, which is not the same as 'exited'.