03 / MODEL SYSTEMS AND CHANGES · LESSON 11

Preview simulated process stops

Apply process changes only after checking identity, protection and current state.

22 min · learn at your pace

Review intent before changing state

A stop request should identify exactly which process it intends to affect. Host and PID alone are insufficient because the PID may have been reused since the request was prepared. Require the recorded start token to match before considering any change.

This lab models process control with a TSV file. It never calls kill, taskkill or a host-management service. Preview is the default and reports would_stop only for an unprotected, running process with the expected identity. Explicit --apply changes that simulated row to stopped. The same decision structure can guide a real tool, but a live implementation needs an authoritative identity check immediately before acting.

Preserve rejected requests

A stale request, protected process, already-stopped process and missing process are separate outcomes. Retain them in the report rather than hiding unsuccessful changes. Validation order matters: a stale identity must not be presented as the current process simply because its protection flag is also set.

Apply requests in their supplied order. A duplicate valid stop request then sees already_stopped after the first change, making repeated use understandable. Preserve process-file ordering and all unrelated rows. Preview must leave the file byte-for-byte unchanged, so a user can inspect the proposed actions without accidentally performing them. The next lesson extends this pattern to explicit jobs across a simulated fleet.

Try an example.sh
awk -F '\t' 'NR>1 {printf "%s PID %s: %s (protected=%s)\n",$1,$2,$5,$4}' processes.tsv
YOUR TURN

Put it into practice.

processes.tsv contains unique host/PID rows with header host<TAB>pid<TAB>start_token<TAB>protected<TAB>state (yes/no; running/stopped). requests.tsv has host<TAB>pid<TAB>start_token. Default --preview; accept --apply; other modes exit 2 with “Invalid mode” and no changes. Print host<TAB>pid<TAB>result in request order. Decide in order: missing pair; stale token; protected=yes; already_stopped; otherwise would_stop in preview, or stopped in apply and update the virtual row. Requests observe earlier applied changes. Preserve process ordering and all other values. Preview must not rewrite the file; use no real process-control commands.

See an explained solution
One way to solve it.sh
mode=${1:---preview}
case "$mode" in --preview|--apply) ;; *) printf 'Invalid mode\n' >&2;exit 2;; esac
work=$(mktemp -d .stop.XXXXXX) || exit 2
trap 'rm -rf "$work"' EXIT
awk -F '\t' -v apply="$mode" -v staged="$work/processes" '
FILENAME==ARGV[1]{if(FNR>1){key=$1 SUBSEP $2;order[++n]=key;host[key]=$1;pid[key]=$2;token[key]=$3;protect[key]=$4;state[key]=$5}next}
FNR>1{
 key=$1 SUBSEP $2;result="missing"
 if(key in state){
  if(token[key]!=$3)result="stale"
  else if(protect[key]=="yes")result="protected"
  else if(state[key]=="stopped")result="already_stopped"
  else if(apply=="--apply"){state[key]="stopped";result="stopped"}
  else result="would_stop"
 }
 printf "%s\t%s\t%s\n",$1,$2,result
}
END{
 print "host\tpid\tstart_token\tprotected\tstate" > staged
 for(i=1;i<=n;i++){key=order[i];printf "%s\t%s\t%s\t%s\t%s\n",host[key],pid[key],token[key],protect[key],state[key] >> staged}
}' processes.tsv requests.tsv > "$work/rows"
printf 'host\tpid\tresult\n'
cat "$work/rows"
if [ "$mode" = --apply ];then mv "$work/processes" processes.tsv;fi

The preview and apply paths share one decision order. Identity and protection checks precede mutation, while preserving rejected outcomes gives reviewers a complete account of what changed and why other requests did not.