TT Lab
Blog Get started
Learn Learning paths Courses

Linux Fundamentals

Working With Processes and Signals

Continue in TT Lab

Goal

You send signals to processes to see how stopping, terminating, and force-killing each differ. In the last two steps you use evidence to untangle the incidents "I sent it and it won't die" and "I killed it and it came back".

Why it matters

A report that "the process won't die" lumps together at least four different situations. The application may have caught SIGTERM and ignored it, it may be in the D state so that signal handling itself was postponed, or it may already be dead and left behind as a zombie. The responses are all different, so you should first ask at which layer it stopped. And the rule that only SIGKILL and SIGSTOP cannot be caught is the last resort the kernel leaves to the operator; that is why SIGKILL gives no chance to clean up and, for a DB or a queue consumer, is the same as pulling the plug.

The incident steps are where you actually make that judgment. The same "it did not die" splits into a signal pending in a stopped state, a handler ignoring it, and the wrong PID, and covering all three with -9 cuts off queue-using jobs without cleanup. "It comes back" is solved only by following the parent-child relationship upward. That is why the grader checks not only whether the process disappeared but also whether it exited cleanly and whether processes that must stay alive are untouched.

Steps

  1. Start sleep 3600 in the background and write its PID to /root/proc/pid.txt.
  2. Write that process's parent PID to /root/proc/ppid.txt.
  3. Start another sleep and stop it (SIGSTOP), then write its PID to /root/proc/stopped_pid.txt and the single state letter after stopping to /root/proc/state_stopped.txt.
  4. Start another sleep, write its PID to /root/proc/killed_pid.txt, and then terminate it with SIGTERM.
  5. Create /root/proc/trapper.sh. When it receives SIGTERM, it must add one line to /root/proc/term.log: caught SIGTERM. Make it executable, start it in the background, send it SIGTERM, and check that the log accumulates.
  6. Start trapper.sh again and this time send SIGKILL. Write the number of lines in term.log just before sending to /root/proc/before_kill.txt and the number of lines just after sending to /root/proc/after_kill.txt. When the lab ends, no trapper.sh may be left alive.
  7. Create /root/proc/cleanup.sh <패턴> (pattern). It must terminate every process whose command line matches the pattern and print the number of terminated processes as one line in the form terminated=<n>. It must neither count nor kill itself.
  8. Incident lb-term-ignored: SIGTERM was sent to three jobs, but none of them went down. Pick and record the cause for each job, and take all three down, making b and c, which use a queue, exit cleanly.
  9. Incident lb-respawn: when you kill crunch, which eats CPU, it comes back with only the PID changed. Pick and record the cause, and take crunch down permanently while keeping log-shipper alive.

Notes

Start a background process

Start sleep 3600 in the background and write its PID to /root/proc/pid.txt.

Put & after the command to send it to the background. Right after that, $! holds its PID.

Find the parent PID

Write that process's parent PID to /root/proc/ppid.txt.

The kernel records the parent PID for every process. Find the line meaning parent in the status file under /proc//, or ask ps to show the columns you choose. You already wrote the PID from step 1 to a file.

Stop a process

Start another sleep and stop it (SIGSTOP), then write its PID to /root/proc/stopped_pid.txt and the single state letter after stopping to /root/proc/state_stopped.txt.

Stopping and terminating are different. A stopped process stays in the list and its state letter changes.

Politely ask a process to terminate

Start another sleep, write its PID to /root/proc/killed_pid.txt, and then terminate it with SIGTERM.

The command that sends signals sends SIGTERM unless you give a number or name. Once the process is dead there is no way to find its PID, so write it to a file before sending.

A script that catches SIGTERM

Create /root/proc/trapper.sh. When it receives SIGTERM, it must add one line to /root/proc/term.log: caught SIGTERM. Make it executable, start it in the background, send it SIGTERM, and check that the log accumulates.

The shell has a built-in command for setting up "when this signal comes, run this command". If the script ends right after setting it up, it never gets the chance to receive the signal, so you need a part that waits without exiting. After starting it, send a termination request to its PID and check the log file.

SIGKILL cannot be caught

Start trapper.sh again and this time send SIGKILL. Write the number of lines in term.log just before sending to /root/proc/before_kill.txt and the number of lines just after sending to /root/proc/after_kill.txt. When the lab ends, no trapper.sh may be left alive.

Send 9 to the script that has the trap set, and record the line count before and after to compare whether it grew.

A pattern-based cleanup script

Create /root/proc/cleanup.sh <패턴> (pattern). It must terminate every process whose command line matches the pattern and print the number of terminated processes as one line in the form terminated=<n>. It must neither count nor kill itself. The grader starts test processes and actually runs the script.

A tool that finds processes by command line (pgrep's -f) searches for the pattern in the entire command line, so this script itself, which received the pattern as an argument, matches too. Get the list first, then send a termination request to each one while skipping your own PID, count them, and print the count at the end. If you capture the list with command substitution, the command line of that subshell matches too; see the explanation in the format example below.

Incident: SIGTERM was sent, but none of the three went down

Running incident lb-term-ignored raises the incident. The deployment owner sent SIGTERM to each PID written in the pid files under /srv/lbproc/run/, but none of the jobs lbjob-a, lbjob-b, and lbjob-c went down (they remain in the list, or the log /srv/lbproc/log/<작업>.log (job) keeps growing). Find the cause for each job and take all three down. b and c, which use a queue, must exit cleanly: clean shutdown must appear in the log, and you must not cut them off with SIGKILL. a is a stateless job, so you may take it down any way you like. In /root/incident/lb-term-ignored/report.txt, write three lines, lbjob-a: <코드>, lbjob-b: <코드>, and lbjob-c: <코드>, plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Each job's code is one of the following: term-ignored (catches TERM and ignores it), stopped-pending (TERM is pending because the process is stopped), wrapper-only (only the wrapper shell died and the real worker survived), zombie (already dead but not reaped), d-state (uninterruptible wait inside the kernel).

Direction: split "it did not die" into three: did the signal arrive and get handled but ignored, did it arrive but not get handled yet, or did the wrong process receive it. Do not assume all three have the same cause. Tools: ps, pgrep, /proc//status, the job logs. Concretely: look at the State (S waiting, T stopped, Z zombie, D uninterruptible), ShdPnd and SigPnd (pending signals; TERM is the 4000 bit), SigCgt (signals with handlers), and PPid (parent) lines of /proc/<PID>/status. If the PID in the pid file no longer exists, search by command line for a process doing the same work under a different PID and look at its parent. Look at what that process's state field is, and how a signal that arrives in that state is handled (handled right away, or left pending). Anything left in the list as <defunct> (Z) after you take it down has already finished; PID 1 in this environment just does not reap it.

Incident: you kill it, and it comes back with only the PID changed

Running incident lb-respawn raises the incident. When you kill crunch (/srv/lbops/bin/crunch.py), which eats CPU, it is back 1–2 seconds later under a different PID (the same happens with -9). Find the cause and take crunch down permanently (so it does not come back). log-shipper on the same server must keep running, and its PID must not change. In /root/incident/lb-respawn/report.txt, write cause: <코드> and evidence: <관찰한 출력 한 줄> (one line of output you observed). The code is one of the following: respawned-by-parent (a parent process restarts it every time it exits), cron-job (a periodic job restarts it), term-ignored (it does not die because it ignores the signal), multiple-instances (several were running from the start).

Direction: "who started it" is recorded for every process. Follow the parent of the restarted crunch upward. Also check what happens to the child after you take down the parent. Tools: ps, /proc//status. Concretely: the PPid line of /proc/<PID>/status (or ps's parent PID column) is the parent. The PID of the restarted crunch is written anew to /srv/lbops/run/crunch.pid. Looking at the parent's command line and the content of its script shows what it repeats. If a shell dies from TERM while waiting for its child, the child is left as an orphan (PPid 1). Killing everything at once by name pattern may catch log-shipper too.