هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- Make sure cron runs (package
cronieon RHEL,cronon Ubuntu):
The first line is for RHEL, the second for Ubuntu.
- Learn the time format with examples:
| Line | Runs |
|---|---|
*/15 * * * * | Every 15 minutes. |
0 * * * * | Every hour, at minute 0. |
30 2 * * * | Every day at 02:30. |
0 6 * * 1-5 | Monday to Friday at 06:00. |
0 3 1 * * | On the 1st of every month at 03:00. |
@reboot | Once after every boot. |
- Write the script first and test it by hand — with full paths, because cron has a very short
PATH:
#!/bin/bash
set -euo pipefail
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
tar -czf /backup/etc-$(date +%F).tar.gz -C / etc
find /backup -name 'etc-*.tar.gz' -mtime +14 -deletechmod 750 /usr/local/bin/backup.sh
/usr/local/bin/backup.sh- **System jobs: a file in
/etc/cron.d/(easy to manage with Ansible or Git; it has a user** field):
SHELL=/bin/bash
MAILTO=""
30 2 * * * root flock -n /run/backup.lock /usr/local/bin/backup.sh >> /var/log/backup/backup.log 2>&1flock -n makes sure the job never runs twice at the same time (if the last run is still busy, the new one is skipped).
- **The
%trap:** in a crontab line,%means "new line". Escape it, or put the command in a script:
0 1 * * * root /usr/bin/mysqldump shop > /backup/shop-$(date +\%F).sql- Who may use cron:
/etc/cron.allowand/etc/cron.deny. Never put secrets in crontab lines (other admins and backups see them) — read them from a file with600rights.
Check that it worked
Set a test job for the next minute, then look at the cron log and the job's own log:
journalctl -u crond --since "-10 min"
journalctl -u cron --since "-10 min"
tail /var/log/backup/backup.logYou see a line with CMD (/usr/local/bin/backup.sh) at the right time. Then put the real time back.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Works by hand, not in cron | Cron has a short PATH and no profile (.bashrc). | Full paths in the script, or PATH= at the top. |
| The job never starts, nothing in the log | Syntax error in the line, or a % in it. | Check the 5 fields; escape % as \%. |
| No output anywhere | Output went to local mail (or nowhere). | Add >> /var/log/... 2>&1. |
A file in /etc/cron.d/ is ignored | Wrong file name (with a dot) or wrong rights, or no user field. | Name without dots, owned by root, 644, add the user field. |
| Two copies of the job run | The job takes longer than the interval. | flock -n /run/NAME.lock COMMAND. |
| Wrong time | The server time zone is not what you think. | timedatectl; cron uses the system time zone. |
| The job did not run while the server was off | cron does not catch up. | Use anacron (/etc/cron.daily) or a systemd timer with Persistent=true. |
Go further
- Command pages: crontab, systemctl, journalctl, timedatectl.
- Next how-tos: Create a systemd timer, Create and extract tar.gz archives, Read the logs of a service, Write a safe Bash script, Run a command once at a set time.
- Learn it: RHCSA — Cron time fields, RHCSA — crontab, RH134 — User crontab, RH134 — The cron environment.
- At work: crontab in daily DevOps.
- Practise: Lab: schedule jobs with cron, at and a timer, Lab: a day on call.
1Why does a script work in your shell but not from cron?
Cron runs with a minimal environment (short PATH, no .bashrc); use full paths or set PATH in the script.
2How do you stop a cron job from running twice at the same time?
Start it with flock -n /run/NAME.lock.