Product SiteDocumentation Site

9.7. جدولة المهام باستخدام cron وatd

cron is the daemon responsible for executing scheduled and recurring commands (every hour, every day, every week, etc.). atd deals with commands to be executed a single time, but at a specific moment in the future.
في نظام يونكس، هناك العديد من المهام التي تستدعى بانتظام:
افتراضياً، يستطيع جميع المستخدمون جدولة تنفيذ المهام. لكل مستخدم إذن crontab خاص به يستطيع فيه تسجيل الأوامر المجدولة. يمكن تحريره بالأمر crontab -e (تخزن محتوياته في الملف /var/spool/cron/crontabs/user).
يملك المستخدم الجذر crontab خاص به، لكنه يستطيع أيضاً استخدام الملف /etc/crontab، أو كتابة ملفات crontab إضافية في المجلد /etc/cron.d. يمتاز الحلان الأخيران بأنه يمكن فيهما تحديد هوية المستخدم التي سوف تستخدم عند تنفيذ الأوامر.
تتضمن الحزمة cron افتراضياً بعض الأوامر المجدولة التي يتم تنفيذها:
تعتمد العديد من حزم دبيان على هذه الخدمة: حيث تضمن من خلال وضع سكربتات الصيانة في هذه المجلدات العمل الأمثل لخدماتها.

9.7.1. صيغة ملف crontab

Each significant line of a crontab entry describes a scheduled command with the six (or seven) following fields:
  • the value for the minute (from 0 to 59);
  • قيمة للساعات (عدد يتراوح بين 0 و 23)؛
  • قيمة لتاريخ اليوم من الشهر (من 1 إلى 31)؛
  • قيمة للشهر (من 1 حتى 12)؛
  • قيمة للنهار من الأسبوع (من 0 إلى 7، 1 يرمز لنهار الإثنين، ويرمز لنهار الأحد بالرقم 0 والرقم 7؛ من الممكن أيضاً استخدام الحروف الثلاثة الأولى من اسم اليوم بالإنكليزية، مثل Sun، أو Mon، الخ)؛
  • اسم المستخدم الذي ستنفذ الأوامر تحت هويته (في الملف /etc/crontab وفي الملفات المجزئة في المجلد /etc/cron.d/، لكن ليس في ملفات crontab الخاصة بالمستخدمين)؛
  • الأمر المراد تنفيذه (عند تحقق الشروط المعرفة في الحقول الخمسة الأولى).
كل هذه التفاصيل موثقة في صفحة الدليل crontab(5)‎.
Each value can also be expressed in the form of a list of possible values (separated by commas). The syntax a-b describes the interval of all the values between a and b. The syntax a-b/c describes the interval with an increment of c (example: 0-10/2 means 0,2,4,6,8,10). An asterisk * is a wildcard, representing all possible values.

مثال 9.2. Sample user crontab file

#Format
#min hour day mon dow  command

# Download data every night at 7:25 pm
 25  19   *   *   *    $HOME/bin/get.pl

# 8:00 am, on weekdays (Monday through Friday)
 00  08   *   *   1-5  $HOME/bin/dosomething

# every two hours
 *  */2   *   *   *    $HOME/bin/dosomethingelse

# Restart the IRC proxy after each reboot
@reboot /usr/bin/dircproxy

9.7.2. استخدام الأمر at

The at executes a command at a specified moment in the future. It takes the desired time and date as command-line parameters, and the command to be executed in its standard input. The command will be executed as if it had been entered in the current shell. at even takes care to retain the current environment, in order to reproduce the same conditions when it executes the command. The time is indicated by following the usual conventions: 16:12 or 4:12pm represents 4:12 pm. The date can be specified in several European and Western formats, including DD.MM.YY (27.07.22 thus representing 27 July 2022), YYYY-MM-DD (this same date being expressed as 2022-07-27), MM/DD/[CC]YY (i.e., 12/25/22 or 12/25/2022 will be December 25, 2022), or simple MMDD[CC]YY (so that 122522 or 12252022 will, likewise, represent December 25, 2022). Without it, the command will be executed as soon as the clock reaches the time indicated (the same day, or tomorrow if that time has already passed on the same day). You can also simply write “today” or “tomorrow”, which is self-explanatory.
$ at 09:00 27.07.22 <<END
> echo "Don't forget to wish a Happy Birthday to Raphaël!" \
>   | mail lolando@debian.org
> END
warning: commands will be executed using /bin/sh
job 1 at Wed Jul 27 09:00:00 2022
هناك صيغة بديلة تستخدم لتأجيل التنفيذ لفترة محددة: at now + number period. يمكن أن تكون الفترة period دقائق (minutes)، أو ساعاتٍ (hours)، أو أياماً (days)، أو أسابيعاً (weeks). يبين العدد number ببساطة عدد الوحدات المذكورة التي يجب أن تنقضي قبل تنفيذ الأمر.
لإلغاء مهمة مجدولة باستخدام cron، استدع crontab -e ببساطة واحذف السطر الموافق في ملف crontab. أما بالنسبة لمهام at، فالعملية بنفس السهولة تقريباً: فقط استدع atrm task-number. يعطيك at رقم المهمة عند جدولتها، لكن يمكنك الحصول عليه ثانية باستخدام الأمر atq، الذي يعطي لائحة محدّثة بالمهام المجدولة.