diff --git a/module/service.sh b/module/service.sh index 3cbdf1f..3001713 100644 --- a/module/service.sh +++ b/module/service.sh @@ -25,6 +25,8 @@ d="${0%/*}"; [ "$d" = "$0" ] && d="."; MODDIR="$(cd "$d" 2>/dev/null && pwd || e STATE_DIR=/data/adb/vdbackup LOGS="$STATE_DIR/logs" LOOPPID="$LOGS/scheduler.loop.pid" +LOOPBOOT="$LOGS/scheduler.loop.boot" +BOOTID="$(cat /proc/sys/kernel/random/boot_id 2>/dev/null)" INTERVAL=900 # wait for boot to complete before touching the framework @@ -48,11 +50,15 @@ mkdir -p "$LOGS" # honour the skip flag [ -f "$MODDIR/.scheduler.skip" ] && exit 0 -# single-instance guard: bail out if a scheduler loop is already alive -if [ -f "$LOOPPID" ] && kill -0 "$(cat "$LOOPPID" 2>/dev/null)" 2>/dev/null; then exit 0; fi +# single-instance guard: bail out if a scheduler loop is already alive. The pid file survives a +# reboot and Android reuses pids quickly, so a bare kill -0 can match an unrelated process and make +# this script exit before the scheduler and the VDLU boot update; only trust a pid recorded in the +# current boot (boot_id). +if [ -f "$LOOPPID" ] && [ "$(cat "$LOOPBOOT" 2>/dev/null)" = "$BOOTID" ] && kill -0 "$(cat "$LOOPPID" 2>/dev/null)" 2>/dev/null; then exit 0; fi # recurring, detached heartbeat (survives boot, never blocks it): the loop # records its own pid for the guard, then runs scheduler-tick every INTERVAL. +echo "$BOOTID" > "$LOOPBOOT" setsid nohup sh -c ' echo "$$" > "$1" while :; do diff --git a/module/uninstall.sh b/module/uninstall.sh index 0f74437..bc6f808 100644 --- a/module/uninstall.sh +++ b/module/uninstall.sh @@ -30,7 +30,11 @@ STATE_DIR=/data/adb/vdbackup # stop the detached scheduler heartbeat first, so it does not keep spinning every 900s # against a helper.sh that uninstall is about to remove (harmless but noisy until reboot). _lp="$(cat "$STATE_DIR/logs/scheduler.loop.pid" 2>/dev/null)" -case "$_lp" in ''|*[!0-9]*) : ;; *) kill "$_lp" 2>/dev/null ;; esac +# only signal the pid when it was recorded in this boot (scheduler.loop.boot == boot_id): the pid file +# survives a reboot and Android reuses pids, so a stale pid could belong to an unrelated process. +_lb="$(cat "$STATE_DIR/logs/scheduler.loop.boot" 2>/dev/null)" +_bid="$(cat /proc/sys/kernel/random/boot_id 2>/dev/null)" +case "$_lp" in ''|*[!0-9]*) : ;; *) [ "$_lb" = "$_bid" ] && kill "$_lp" 2>/dev/null ;; esac rm -rf "$STATE_DIR/staging" "$STATE_DIR/iconcache" 2>/dev/null -rm -f "$STATE_DIR/logs"/*.pid "$STATE_DIR/logs"/*.out "$STATE_DIR/logs"/progress-*.json 2>/dev/null +rm -f "$STATE_DIR/logs"/*.pid "$STATE_DIR/logs"/scheduler.loop.boot "$STATE_DIR/logs"/*.out "$STATE_DIR/logs"/progress-*.json 2>/dev/null exit 0