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

linux crontab daily

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

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

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

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

cat /etc/anacrontab
cat /etc/anacrontab
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
# /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
# /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
man anacrontab
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
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
       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 минутами

Как установить Redis на MAC системе с M1 (семейство процессоров M)

Чтобы установить Редис на Mac компьютер/ноутбук на процессоре M1, необходимо сделать следующее:

  • Открыть Терминал для ввода команд: Launchpad > Other > Terminal.
  • Установить Homebrew. Вводим следующую команду в Терминале и подтверждаем ввод Энтером:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • Если, по окончанию успешного процесса установки увидите сообщение ошибки: Warning: /opt/homebrew/bin is not in your PATH. Необходимо добавить Homebrew в PATH (2е команды):
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') /Users/quest/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') /Users/quest/.bash_profile eval "$(/opt/homebrew/bin/brew shellenv)"
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') /Users/quest/.bash_profile 
eval "$(/opt/homebrew/bin/brew shellenv)"
  • Затем, обновляем Homebrew:
brew update
brew update
brew update
  • Теперь, мы готовы к непосредственной установке Редиса:
brew install redis
brew install redis
brew install redis
  • Когда установка будет завершена, стартуем сервис Redis:
brew services start redis
brew services start redis
brew services start redis
  • Чтобы проверить корректную установку, «пропингуем» сервис:
redis-cli ping
redis-cli ping
redis-cli ping

В ответе на нашу команду, если Редис установлен и запущен, мы увидим «PONG» в Терминале.

Это все. Мы установили, запустели и проверили установку Redis на вашем Mac M1.

How to install Redis on Mac with M1

You can follow these steps to install Redis on your Mac M1:

  • Open Terminal by going to Launchpad > Other > Terminal.
  • Install Homebrew by pasting this command in the Terminal and pressing Enter:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • If you see error message: Warning: /opt/homebrew/bin is not in your PATH, run the following 2 commands:
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') /Users/quest/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') /Users/quest/.bash_profile eval "$(/opt/homebrew/bin/brew shellenv)"
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') /Users/quest/.bash_profile
eval "$(/opt/homebrew/bin/brew shellenv)"
  • Once Homebrew is installed, update it by running this command:
brew update
brew update
brew update
  • Now you can install Redis by running this command:
brew install redis
brew install redis
brew install redis
  • After the installation is complete, start the Redis server by running this command:
brew services start redis
brew services start redis
brew services start redis
  • To check if Redis is running, you can run the command:
redis-cli ping
redis-cli ping
 redis-cli ping 

If Redis is running, it will return «PONG» in the Terminal.

That’s it! Redis is now installed and running on your Mac M1.