Learn about the basics of cron
table of contents
This is Nakagawa from the System Solutions Department.
This time I will write about the basics of cron.
What is cron?
A UNIX-based OS daemon process that executes instructions at a registered time.
"Run XX command every day" "Run XX command every hour" The
command will be automatically executed at the time you set.
How to write
minutes | time | day | month | week | Example) Execution command |
0~59 | 0~23 | 1~31 | 1~12 | 0~7 | echo "test01" >> test.txt |
-Basically, as shown above, from left to right: "minute", "hour", "day", "month", "week", and "execution command".
actually write it
* * * * * echo "test01" >> /var/www/html/CronTest_01.txt ## Run every minute
・If you set "*", all will be selected.In other words,
it will be "the process of writing test01 to /var/www/html/CronTest_01.txt every minute".
Save this file and after 3 minutes...
$ less /var/www/html/CronTest_01.txt
test01 test01 test01
In this way, the CronTest_01.txt file is created and 3 lines are written because cron was executed 3 times.
10 * * * * echo "test02" >> [Execution command] ## Execute at the 10th minute of every hour
The command you set will be executed at 10:00.
0 15 26 * * echo "test03" >> [Execution command] ## Execute at 15:00 on the 26th
The command is executed even though I set it to 15:00 on the 26th.
10 20 * 1 * echo "test04" >> [Execution command] ## Execute at 20:10 in January
The command is executed even though I set it to 20:10 in January.
The week can be set as a number from 0 to 7, as shown below.
day | month | fire | water | tree | gold | soil |
0 or 7 | 1 | 2 | 3 | 4 | 5 | 6 |
0 3 * * 7 echo "test05" >> /var/www/html/CronTest_05.txt ## Run on Sunday at 3:00
The command will be executed when set to 3:00 on Sunday.
30-40 * * * * [Execution command]
In addition, if you write as above... The command set in 1 minute increments will be executed from 30 minutes to 40 minutes.
I think now you can also read when cron is executed even if it is set by someone other than yourself.
summary
I often check cron settings during operation,
for example, if the load is increasing, it may be due to heavy processing set to cron.
If the load suddenly increases at a timing like 〇〇:00, or
if the cron process is running when you check with the ps command,
looking at the cron settings will give you a clue to the solution. Maybe.