Posts

What is cron? How does it work?


Greetings to all! Jon here from Beyond GTA, and today I am going to share some basic knowledge about cron, which is a very common term for Unix-based operating systems. The article I'm referencing was written by Nakagawa about a few years ago.


What is cron?

The UNIX-like OS daemon process executes commands at preset schedules.

There are commands such as "Execute x command every day", "Execute x command every hour", and so on.


How to write

Minute

Hour

Day

Month

Week

Example

0~59

0~23

1~31

1~12

0~7

echo "test01" >> test.txt

As shown above, the basic writing system consists of the following parts: "minute"nth," "week," and "execution command."

In practice

* * * * * echo "test01" >> /var/www/html/CronTest_01.txt ## run every minute.

The "*" set means that everything is selected, which means that every minute, the process will write test01 to /var/www/html/CronTest_01.txt.


crontab -e
* * * * * echo "test01" >> /var/www/html/CronTest_01.txt  ##run every minute.

If we check the results 3 minutes after saving the setting above, we will see the following:

$ less /var/www/html/CronTest_01.txt


test01
test01
test01

Three lines in CronTest_01.txt have been written since cron was executed three times.

10 * * * * echo"test02">> 【Execution command】 ## Runs every 10 minutes of every hour

Even though it is already set, the command will be executed at 00:10.

0 15 26 * * echo "test03" >> [Execution command] ## Execute at 15:00 on 26th

Every 26th of the month, at 15:00, this command will be executed.


10 20 * 1 * echo "test04" >> [Execute command] ## Execute at 20:10 on Jan.

A time of 8:10 p.m. has been set for execution of the above command in January.

The week can be set to a number from 0~7, as follows


Sun

Mon

Tue

Wed

Thu

Fri

Sat

0 or 7

1

2

3

4

5

The below command will be executed at 3:00 AM on Sunday.

0 3 * * 7 echo "test05" >> /var/www/html/CronTest_05.txt ## Run Sunday at 3:00

As well as that, if you write the following, the command will be executed every 1 minute between 30 and 40 minutes.

30-40 * * * * [Execution command]

Summary

These briefly explain how cron works and its role in operations.

For example, an increase in load could be due to heavy processes specified in the cron settings. If the server load is rising rapidly at a specific time when current processes are checked with the ps command, it might be a cron job running given that time.

A look at the cron settings may provide a possible solution in server operation.






6 views