Scheduling jobs: cron and timers
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A scheduler runs a command for you at set times, so a backup or a clean-up happens every night without anyone typing it. Follow one job, /opt/backup.sh at 02:30, through both Linux schedulers: a crontab line whose five time fields the cron daemon checks against the clock every minute, and a systemd timer that starts a matching service, logs to the journal and can catch up a run the machine missed while it was off.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01When does the crontab line 30 2 * * * /opt/backup.sh run?
The fields are minute, hour, day of month, month, day of week. Minute 30 and hour 2, with * (any value) for the other three, means 02:30 every day.
02It is 02:29. Why does the cron daemon not run the job yet?
The cron daemon wakes once a minute and runs a line only when every time field matches the current time. At 02:29 the minute does not match; one minute later all five do.
03A cron job prints "backup done". Where does that text go by default?
cron collects a job's output and mails it to the owner of the crontab, or to the address in MAILTO. On a machine without mail set up, redirect it to a file instead, for example >> /home/ana/backup.log 2>&1.
04backup.timer has OnCalendar=*-*-* 02:30:00 and no Unit= line. Which unit does it start?
A timer starts the unit with the same name and a different ending unless Unit= says otherwise, so backup.timer starts backup.service. Its output goes to the journal: journalctl -u backup.service.
05The machine is switched off from midnight to 08:15. What happens to the 02:30 run?
cron assumes the machine is always on, so a missed minute is simply missed (anacron exists for that). With Persistent=true, systemd stores the last run time on disk and starts the missed run as soon as the timer is active again.
المسرد
- Scheduler
- A program that runs commands for you at set times, such as cron or systemd timers.
- Daemon
- A program that keeps running in the background with no terminal, waiting for work, such as the cron daemon.
- Crontab
- Your table of cron jobs: one line per job, five time fields and a command. Edit it with crontab -e.
- Time field
- One of the five numbers in a crontab line: minute, hour, day of month, month, day of week. * means any value.
- systemd timer
- A .timer unit file that tells systemd when to start another unit, usually the .service with the same name.
- OnCalendar=
- The timer setting that names a calendar time, such as *-*-* 02:30:00 for 02:30 every day.
- Journal
- systemd's log. A service's output is stored there; read it with journalctl -u <unit>.
- Persistent=true
- A timer setting that saves the last run time on disk, so a run missed while the machine was off happens after boot.
الملاحظات والمصادر
- On Debian and Ubuntu the cron daemon is called cron; on Fedora and RHEL (cronie) it is crond. Both read crontab lines the same way.
- If you restrict both day of month and day of week (neither is *), cron runs the job when either one matches, not only when both do.
- In a crontab line a % sign means a new line, so write \% inside a command such as date +\%F.
- Both schedulers run /opt/backup.sh directly, so the file needs its execute bit: sudo chmod +x /opt/backup.sh. Without it cron mails "/bin/sh: /opt/backup.sh: Permission denied" and systemd logs status=203/EXEC.
- A system service runs as root unless its [Service] section has a User= line. The cron job runs as the owner of the crontab.
- Linux man-pages: crontab(5), the crontab line format
- Linux man-pages: crontab(1), crontab -e, -l and -r
- Linux man-pages: cron(8), the cron daemon
- Linux man-pages: anacron(8)
- Linux man-pages: systemd.timer(5)
- Linux man-pages: systemd.time(7), calendar events
- Linux man-pages: systemd.service(5)
- Linux man-pages: systemd.exec(5), StandardOutput=
- Linux man-pages: systemctl(1), list-timers
- Linux man-pages: journalctl(1)
- ArchWiki: systemd/Timers
- Debian manpages: cron(8)
اختبار يتاح بعد الخطوة الأخيرة.