Как работает cron с переменными времени @daily, @weekly, @monthly

linux crontab daily

Простыми словами, переменные расписания запускают команды:

  • @daily — раз в день
  • @weekly — раз в неделю
  • @monthly — раз в месяц

Как же понять, в какое именно время запускаются команды?..

…Заходим в консоль сервера и используем команду:

cat /etc/anacrontab

Вывод примерно такой:

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly

Пояснения к этой системе можно найти, введя команду:

man anacrontab

Вывод будет примерно такой:

       The  START_HOURS_RANGE  variable  defines an interval (in hours) when scheduled jobs can be run.  In case this time interval is missed, for example, due to a power down, then scheduled jobs are not executed that day.

       The RANDOM_DELAY variable denotes the maximum number of minutes that will be added to the delay in minutes variable which is specified for each job.  A RANDOM_DELAY set to 45 would  therefore add, randomly, between 0 and 45 minutes to the delay in minutes for each job in that particular anacrontab.  When set to 0, no random delay is added.

       Empty lines are either blank lines, line containing white spaces only, or lines with white spaces followed by a '#' followed by an arbitrary comment.

       You can continue a line onto the next line by adding a '\' at the end of it.

       In case you want to disable Anacron, add the 0anacron cron job (which is a part of crontab(1)) into the /etc/cron.hourly/jobs.deny directory.

EXAMPLE
       This example shows how to set up an Anacron job similar in functionality to /etc/crontab which starts all regular jobs between 3:00 and 22:00 only.  A RANDOM_DELAY which can be 45 minutes at the most is specified.  Jobs will run serialized in a queue where each job is started only after the previous one is finished.

       # environment variables
       SHELL=/bin/sh
       PATH=/sbin:/bin:/usr/sbin:/usr/bin
       MAILTO=root
       RANDOM_DELAY=45
       # Anacron jobs will start between 6am and 8am.
       START_HOURS_RANGE=3-22
       # delay will be 5 minutes + RANDOM_DELAY for cron.daily
       1         5    cron.daily          nice run-parts /etc/cron.daily
       7         0    cron.weekly         nice run-parts /etc/cron.weekly
       @monthly  0    cron.monthly        nice run-parts /etc/cron.monthly

Пояснения:

  • Если запускается несколько команд с одной и той же переменной времени (например, @daily), они все ставятся в очередь, сереализуются и выполняются по порядку, при этом
  • START_HOURS_RANGE=3-22 — значит, что команды выполняются только во временном интервали с 3:00 до 22:00
  • RANDOM_DELAY=45 — значит, что следующая команда выполнится только после окончания предыдущей, и через 5 минут (из таблицы с примерами команд) + рандомное количество минут между 0 и 45. Другими словами, через рандомное время между 5 и 50 минутами

Как работает cron с переменными времени @daily, @weekly, @monthly: 1 комментарий

  1. Thanx for the effort, keep up the good work Great work, I am going to start a small Blog Engine course work using your site I hope you enjoy blogging with the popular BlogEngine.net.Thethoughts you express are really awesome. Hope you will right some more posts.

Добавить комментарий

Ваш адрес email не будет опубликован.