Ray's Knowledge Base
Unlock

kill -0 on a root-owned process fails for a normal user, so a wait loop stops too early

PitfallVerified 1 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A stop script starts tools with setsid sudo -n perf ... &, saves $!, later sends them SIGINT, and waits with kill -0 $pid until they exit. Run as a normal user, that loop ends at once, so the next step can read empty or half-written output files.

Cause#

The saved pid is a root process (sudo, then the tool). A normal user may not signal it, so kill -0 returns non-zero with "Operation not permitted". The loop reads that as "the process is gone".

Fix#

Check liveness with the same rights you used to start it:

for p in "$d"/*.pid; do sudo -n kill -INT "$(cat "$p")" 2>/dev/null || true; done
for i in $(seq 60); do
  alive=0
  for p in "$d"/*.pid; do sudo -n kill -0 "$(cat "$p")" 2>/dev/null && alive=1; done
  [ "$alive" = 0 ] && break
  sleep 1
done

Under set -e, end each signal command with || true: a pid that already exited fails the line.

Evidence#

Caught in review of the dpucrumbs vendor-tool observers (perf stat, record and sched started as root around each repetition) before the first run, from the Unix rule that a user may not signal another user's process. With sudo -n kill -0 the loop then waited until perf printed "Captured and wrote ... perf.data", and the files were complete.

Also found by searching

  • kill: (1234) - Operation not permitted
  • wait loop thinks a sudo process already exited
  • how do I check if a root process is still alive from a normal user
  • stop script moves on before perf finished writing its file