Я знаю, что если я написал в crontab -e
следующую команду 01 04 * * * somecommand
, то она будет выполняться somecommand
в 4:01 каждый день каждого месяца.
Что произойдет, если я напишу * * * * * somecommand
? Будет ли он работать somecommand
каждую минуту? Будет ли этот синтаксис работать также?
И можно использовать специальные строки, такие как @reboot
, @daily
и т. Д., Как объяснено здесь . В какое время суток somecommand
будет выполнено, если я напишу @daily somecommand
команду?
Это будет выполнять Вашу команду каждую минуту. Это - допустимый синтаксис.
Вот окровавленные детали от info crontab
:
The first five fields shall be integer patterns that specify the
following:
1. Minute [0,59]
2. Hour [0,23]
3. Day of the month [1,31]
4. Month of the year [1,12]
5. Day of the week ([0,6] with 0=Sunday)
Each of these patterns can be either an asterisk (meaning all valid
values), an element, or a list of elements separated by commas. An ele‐
ment shall be either a number or two numbers separated by a hyphen
(meaning an inclusive range). The specification of days can be made by
two fields (day of the month and day of the week). If month, day of
month, and day of week are all asterisks, every day shall be matched.
If either the month or day of month is specified as an element or list,
but the day of week is an asterisk, the month and day of month fields
shall specify the days that match. If both month and day of month are
specified as an asterisk, but day of week is an element or list, then
only the specified days of the week match. Finally, if either the month
or day of month is specified as an element or list, and the day of week
is also specified as an element or list, then any day matching either
the month and day of month, or the day of week, shall be matched.
Статья, с которой Вы связались, похожа на хороший. Это дает Вам некоторые хорошие примеры, и на самом деле легче читать, чем выборка страницы справочника, которую я предоставил здесь.
Необходимо смочь использовать синтаксис, о котором это говорит.
Согласно моему crontab, @daily работает в 6:25.
$ grep daily /etc/crontab
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
Вы также можете указать нечетные частоты, используя косую черту в каждом столбце.
* */2 * * * foo
будет выполнять foo
в часы, которые делятся на 2, а именно: 12:00, 2:00, 4:00, ..., 10:00, 12:00.
Запомните эту команду
*/1 * * * * env > /home/yourUser/env.out
выведет переменные окружения среды crontab, с которыми вы будете работать, в вашем crontab. Вы можете использовать переменные типа $ {HOME}, $ {SHELL}, чтобы сделать скрипт более чистым, и, возможно, использовать скрипт на другом компьютере.