ai-coding-minesIndexGitHub

Cron + a `pgrep` duplicate-run guard catches itself

Git and automation

Symptom

The job never runs. Not once. No error, no log. Cron records a clean exit 0.

Cause

The shell cron spawns has the script name on its command line, so pgrep hits every time → "already running" → immediate exit, every run.

Fix — PID file + kill -0

PIDFILE=/tmp/job.pid
if [ -f "$PIDFILE" ] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; then exit 0; fi
echo $$ > "$PIDFILE"
trap 'rm -f "$PIDFILE"' EXIT INT TERM HUP

kill -0 sends no signal; it only checks existence. A dead PID passes, so a stale file can't block you.

Check all three: 1) the file is non-empty (-s) 2) the content is numeric 3) that PID is alive

Later case — the silence read as "no orders"

The same guard sat on an order-notification loop. Cron logged a clean exit 0 every run, and the stretch with no notifications at all was read as "no notifications = no orders." A second, unrelated cause was blocking the same path that day, so fixing the guard brought nothing back.

When silence has two causes, fixing one changes nothing visible. If the silence survives your fix, suspect "there's another cause" before "the fix was wrong."