متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 18System · Linux

How to schedule a job with cron

Backups at night, a cleanup every hour, a report every Monday: cron runs commands on a schedule, on every Linux server. It is simple, but most cron problems come from the same few traps — a different PATH, the % sign, no logs, and jobs that run twice at the same time. This how-to writes a safe cron job, explains the time format with examples, and shows where to look when a job "did not run".

  • 5 الدروس
  • 6 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

18.1

The quick answer

crontab -e
crontab -l
30 2 * * * /usr/local/bin/backup.sh >> /var/log/backup/backup.log 2>&1

Five time fields, then the command: minute, hour, day of month, month, day of week. This job runs every day at 02:30.

الأوامر في هذا الدرس

18.2

Step by step

  1. Make sure cron runs (package cronie on RHEL, cron on Ubuntu):
systemctl enable --now crond
systemctl enable --now cron

The first line is for RHEL, the second for Ubuntu.

  1. Learn the time format with examples:
LineRuns
*/15 * * * *Every 15 minutes.
0 * * * *Every hour, at minute 0.
30 2 * * *Every day at 02:30.
0 6 * * 1-5Monday to Friday at 06:00.
0 3 1 * *On the 1st of every month at 03:00.
@rebootOnce after every boot.
  1. Write the script first and test it by hand — with full paths, because cron has a very short PATH:
/usr/local/bin/backup.sh
#!/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 -delete
chmod 750 /usr/local/bin/backup.sh
/usr/local/bin/backup.sh
  1. **System jobs: a file in /etc/cron.d/ (easy to manage with Ansible or Git; it has a user** field):
/etc/cron.d/backup
SHELL=/bin/bash
MAILTO=""
30 2 * * * root flock -n /run/backup.lock /usr/local/bin/backup.sh >> /var/log/backup/backup.log 2>&1

flock -n makes sure the job never runs twice at the same time (if the last run is still busy, the new one is skipped).

  1. **The % trap:** in a crontab line, % means "new line". Escape it, or put the command in a script:
/etc/cron.d/db-dump
0 1 * * * root /usr/bin/mysqldump shop > /backup/shop-$(date +\%F).sql
  1. Who may use cron: /etc/cron.allow and /etc/cron.deny. Never put secrets in crontab lines (other admins and backups see them) — read them from a file with 600 rights.

18.3

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.log

You see a line with CMD (/usr/local/bin/backup.sh) at the right time. Then put the real time back.

18.4

Common problems

ProblemCauseFix
Works by hand, not in cronCron 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 logSyntax error in the line, or a % in it.Check the 5 fields; escape % as \%.
No output anywhereOutput went to local mail (or nowhere).Add >> /var/log/... 2>&1.
A file in /etc/cron.d/ is ignoredWrong 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 runThe job takes longer than the interval.flock -n /run/NAME.lock COMMAND.
Wrong timeThe server time zone is not what you think.timedatectl; cron uses the system time zone.
The job did not run while the server was offcron does not catch up.Use anacron (/etc/cron.daily) or a systemd timer with Persistent=true.

18.5

Go further

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.

كل الأوامر في هذا الفصل