A database dump must run every night at 1:30 AM. How do you schedule it with cron so it is reliable?
BeginnerScenarioScheduling, automation and scripting
Answer
Situation read → Cron entry plus everything cron doesn't give you: environment, logging, failure alerts.
What I'd do →
- Write a script with
set -euo pipefailand absolute paths. - Add a system cron file
/etc/cron.d/db-dump:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
[email protected]
30 1 * * * postgres /usr/local/bin/db-dump.sh >> /var/log/db-dump.log 2>&1
- Test by running the script as that user:
sudo -u postgres /usr/local/bin/db-dump.sh.
Why → Cron runs with a minimal PATH and no login profile, which is the number-one reason jobs work by hand but fail in cron.
Interview tip → Mention /etc/cron.d files include a user column, unlike crontab -e.
More on Scheduling, automation and scripting
Convert that nightly cron job to a systemd timer. Why would you?IntermediateA cron job runs every 5 minutes, but sometimes takes 8 minutes and two copies overlap, corrupting data. How do you prevent that?BeginnerYou have 300 servers running the same cron job at midnight, and they hammer a shared API. How do you fix it?IntermediateA job must run on the last day of every month. Cron has no "last day" field. What do you do?IntermediateYou inherited a server and need to know every scheduled job on it. Where do you look?BeginnerA one-off data fix must run at 9 PM tonight, just once. How do you schedule it?Beginner