Interview Prep/Shell Scripting/Troubleshooting: cron and systemd timers

Every run of a cron job logs `another run is in progress, exiting` for hours, even though `ps` shows no copy of the script running. It uses `exec 9>/run/opskit/backup.lock; flock -n 9 || exit 0`.

AdvancedTroubleshootingTroubleshooting: cron and systemd timers
Answer

Likely causes → A flock lock is held as long as any process has that file descriptor open. The script started a background or long-lived child (nohup ... &, an SSH ControlMaster, a helper daemon) that inherited fd 9, and that child is still running after the script exited.

Diagnose →

fuser -v /run/opskit/backup.lock
lsof /run/opskit/backup.lock          # shows which PID holds it
ls -l /proc/<pid>/fd | grep backup.lock

Fix → Stop the holder, and close the lock fd for children that must outlive the script:

some_daemon 9>&- &              # child gets no copy of fd 9
# or scope the lock to one command, so nothing else inherits it:
flock -n /run/opskit/backup.lock /opt/opskit/bin/backup.sh

Prevent → Keep lock fds away from background children, and log the holder's PID in the "already running" message (fuser output), so this is diagnosable in one step.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: cron and systemd timers