File size: 1,303 Bytes
35d269e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Shared pid tracking. Source this; do not execute it.
HERE=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
#
# WHY NOT pgrep -f: `pgrep -f "bash "$HERE"/claude_windows.sh"` matches ANY process
# whose command line contains that string -- including an unrelated shell that merely passes it
# as an argument (a `pgrep -f`/`grep` in another script, a monitoring one-liner). On 2026-09-18
# that produced a false positive that convinced the watchdog a dead scheduler was alive, which
# would have silently stalled an unattended overnight run. A pidfile plus an identity check
# cannot collide that way.
PIDDIR=${PIDDIR:-outputs/inscope/_genlogs}

pid_write() {  # $1 = logical name; records this process and clears it on exit
  mkdir -p "$PIDDIR"
  echo $$ > "$PIDDIR/$1.pid"
  # shellcheck disable=SC2064
  trap "rm -f '$PIDDIR/$1.pid'" EXIT
}

pid_alive() {  # $1 = logical name, $2 = script basename that must own the pid
  local f="$PIDDIR/$1.pid" p
  [ -f "$f" ] || return 1
  p=$(cat "$f" 2>/dev/null) || return 1
  case "$p" in ''|*[!0-9]*) return 1;; esac
  kill -0 "$p" 2>/dev/null || return 1
  # Guard against pid reuse: the live pid must actually be running that script.
  ps -o command= -p "$p" 2>/dev/null | grep -q "$2" || return 1
  return 0
}

pid_of() { cat "$PIDDIR/$1.pid" 2>/dev/null; }