Regarding the Cron format that enables scheduled execution when performing things such as website crawling and server life and death monitoring database backup on a regular basis, setting methods and setting examples on Linux and setting methods on AWS Lambda Here is a summary of setting examples.
I would like to take this opportunity to thank you for solving the problem by borrowing the wisdom of our predecessors, and I am very sorry to say that I will summarize it here as my own memo.
<Minutes> <Time> <Day> <Moon> <曜Day> <command>
--All parameters are required. --The time zone is UTC (Coordinated Universal Time) only and cannot be changed. <font color = # 0000FF> ** To specify as JST (Japan Standard Time), it is necessary to set "-9 hours" to UTC (minus 9 hours). ** --Specifying less than minutes (in seconds) is not supported.
No. | field | value | Wildcard |
---|---|---|---|
1 | Minutes | 0~59 | 「/」(Slash)、「*」(asterisk)「-」(hyphen)、「,」(comma) |
2 | Time | 0~23 | 「/」(Slash)、「*」(asterisk)「-」(hyphen)、「,」(comma) |
3 | Day | 1~31 | 「/」(Slash)、「*」(asterisk)「-」(hyphen)、「,」(comma) |
4 | Moon | 1-12 or JAN-DEC | 「/」(Slash)、「*」(asterisk)「-」(hyphen)、「,」(comma) |
5 | Day of the week | 1-7 or SUN-SAT | 「/」(Slash)、「*」(asterisk)「-」(hyphen)、「,」(comma) |
6 | command | 任意のcommand | (None) |
--Specifying less than minutes (in seconds) is not supported. --When you actually specify it, you do not need to write "" in the wildcard.
No. | letter | Definition | Setting Example |
---|---|---|---|
1 | 「/」(Slash) | Specify the increment | |
2 | 「*」(asterisk) | Specify all values | |
3 | 「-」(hyphen) | Specify the range | 8-For 10, 8/9 and 10 are set. |
4 | 「,」(comma) | Specify additional values | Sunday, Monday, and Tuesday are set for SUN, MON, and TUE, respectively. |
crontab
#8 am daily(UTC)To'backup.py'To run
0 8 * * * source ~/venv_<Project name>/bin/activate; cd ~/venv_<Project name>/<Project name>; python manage.py backup > ~/cron.log 2>&1
#11:45 pm daily(UTC)Reload Nginx to
45 23 * * * sudo systemctl reload nginx.service
#17:30 on the first day of every month(UTC)Let's Renew Encrypt SSL certificate
30 17 1 * * /home/<Superuser>/certbot/certbot-auto renew -q --renew-hook "/usr/bin/sysytemctl reload nginx.service"
#Or
30 17 1 * * /home/<Superuser>/certbot/certbot-auto renew -q --renew-hook "/usr/bin/sysytemctl reload nginx.service"
#Monday-Friday(UTC)Pings a static IP every 5 minutes
0/5 * * MON-FRI * ping <Elastic IP>
#Or
0/5 * * 2-6 * ping <Elastic IP>
cron <Minutes> <Time> <Day> <Moon> <曜Day> <Year>
--All parameters are required. --The time zone is UTC (Coordinated Universal Time) only and cannot be changed. <font color = # 0000FF> ** To specify as JST (Japan Standard Time), it is necessary to set "-9 hours" to UTC (minus 9 hours). ** --Specifying less than minutes (in seconds) is not supported. --When you actually specify it, you do not need to write "" in the wildcard.
No. | field | value | Wildcard |
---|---|---|---|
1 | Minutes | 0~59 | 「/」(Slash)、「*」(asterisk)、「-」(hyphen)、「,」(comma) |
2 | Time | 0~23 | 「W」、「L」、「/」(Slash)、「?」(Question mark)、「*」(asterisk)、「-(hyphen)、「,」(comma) |
3 | Day | 1~31 | 「/」(Slash)、「*」(asterisk)、「-」(hyphen)、「,」(comma) |
4 | Moon | 1-12 or JAN-DEC | 「/」(Slash)、「*」(asterisk)、「-」(hyphen)、「,」(comma) |
5 | Day of the week | 1-7 or SUN-SAT | 「#」(sharp)、「L」、「/」(Slash)、「?」(Question mark)、「*」(asterisk)、「-」(hyphen)、「,」(comma) |
6 | Year | 1970~2199 | 「/」(Slash)、「*」(asterisk)、「-」(hyphen)、「,」(comma) |
--Specifying less than minutes (in seconds) is not supported. --When you actually specify it, you do not need to write "" in the wildcard. --The value for either day or week must be a "?" (Question mark).
No. | letter | Definition | Setting Example |
---|---|---|---|
1 | 「/」(Slash) | Specify the increment | |
2 | 「L」 | Specify "last" | ① |
3 | 「W」 | Specify weekdays | When specified with a date (Example: 3/W, etc.), the weekday closest to the 3rd of the month is set. If the 3rd is Saturday, it will be executed on Friday the day before. If the 3rd is a Sunday, it will be executed on the following Monday. |
4 | 「#」(sharp) | Specify the nth day of the month | 4#If you specify 3, the third Wednesday of the month is set. (* Wednesday = 4th of 7 days a week) |
5 | 「*」(asterisk) | Specify all values | |
6 | 「?」(Question mark) | Do not specify a value | Set with another specified value. As an example, if you specify a specific date, but run it on any day of the week. |
7 | 「-」(hyphen) | Specify the range | 8-For 10, 8/9 and 10 are set. |
8 | 「,」(comma) | Specify additional values | Sunday, Monday, and Tuesday are set for SUN, MON, and TUE, respectively. |
#8 am daily(UTC)To run
cron(0 8 * * ? *)
#12:45 pm daily(UTC)To run
cron(45 12 * * ? *)
#Monday-Friday 5:30 pm(UTC)To run
cron(30 17 ? * MON-FRI *)
#Or
cron(30 17 ? * 2-6 *)
#Monday-Friday(UTC)Runs every 5 minutes
cron(0/5 * ? * MON-FRI *)
#Or
cron(0/5 * ? * 2-6 *)
AWS official website (Lambda): Scheduled expression using Rate or Cron (https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/services-cloudwatchevents-expressions.html) Rule schedule formula (English)
It's easy to forget the time difference between JST and UTC (▲ 9 hours). Even though I want to move it at night at the end of the month, it moves in the daytime at the beginning of the month and an alert is raised! I decided to always look at this manual when setting so that there is no such thing.