Zero-Downtime Cron Refactors on Small VPS Servers
On single-instance servers, replacing cron jobs can accidentally run old and new pipelines together. The safer approach is a staged handoff with lock ownership.
1) Introduce an explicit lock file contract
LOCK=/var/lock/report-job.lock
exec 9>"$LOCK"
flock -n 9 || exit 0
2) Run shadow mode before cutover
./job-new.sh --dry-run --compare ./last-output.json
3) Cut over with rollback switch
ln -sfn /opt/jobs/new /opt/jobs/current
# rollback
ln -sfn /opt/jobs/old /opt/jobs/current
Failure pattern
- Both old and new jobs writing the same destination.
- No lock guard, leading to overlap after slow runs.
- No rollback pointer for emergency reversion.
What to verify
- Only one scheduler path can run at a time.
- Shadow output matches baseline before cutover.
- Rollback completes in one command.