Crontab command format configuration of Linux timer

 

Commands are often used, but the time format is sometimes not remembered, so let’s record it here.

crontab -l List crontab files

crontab -e edit 

Note: % is considered as newline in crontab, use \ to escape; for example, if there is "date +%Y%m%d", it must be replaced with "date +\%Y\%m\%d"

crontab -r delete   If you do not want to delete the written crontab file, add # in front of the crontab file to comment out the file.

 

Output configuration in crontab:

In crontab, the output of running scripts is often configured as: >/dev/null 2>&1 to avoid content output in crontab operation.

The result of the shell command can be defined by the form of '>'

/dev/null represents an empty device file

> represents where to redirect to, for example: echo "123" > /home/123.txt

1 means stdout standard output, the system default value is 1, so ">/dev/null" is equivalent to "1>/dev/null"

2 means stderr standard error

& means equivalent, 2>&1, means that the output redirection of 2 is equivalent to 1, and the above means that it is also output to an empty device;

 

In particular, pay attention to the configuration of the timing time :

Column 1 minutes 1 to 59

Column 2 hours 1 to 23 (0 means zero)

3rd Liege 1-31

4th column month 1-12

Column 5 Week 0~6 (0 means Sunday)

 

Format of crontab:

Commands that need to be run for time-sharing day, month and week

 

Here are some examples:

30 21 * * * rm /admin/logs/error.log

The above example means to delete the specified log at 21:30 every night

 

45 4 1,10,22 * * rm /admin/logs/error.log

The above example means to delete the specified log at 4:45 on the 1st, 10th, and 22nd of each month

 

10 1 * * 6,0 rm /admin/logs/error.log

The above example means to delete the specified log every Saturday and Sunday at 1:10

 

0,30 18-23 * * * rm /admin/logs/error.log

The above example means to delete the specified log every 30 minutes between 18:00 and 23:00 every day

 

0 23 * * 6 rm /admin/logs/error.log

The above example means to delete the specified log every Saturday at 11:00 pm

 

0 */1 * * * rm /admin/logs/error.log

Delete the specified log every hour

* */1 * * * rm /admin/logs/error.log ( equivalent to unlimited time at the smallest granularity * * * * * sh )

Delete the specified log every minute

 

0 23-7/1 * * * rm /admin/logs/error.log

Between 11:00 pm and 7:00 am, delete the specified log every hour

 

0 11 4 * mon-wed rm /admin/logs/error.log

Delete the specified log on the 4th of every month and every Monday to Wednesday at 11:00

 

0 1 1 jan * rm /admin/logs/error.log

Delete the specified log at 1:00 on the 1st of January

 

Note: There is a blue note point above, which is the relationship between different units of time.

 

For example, to check the log every 5 minutes, we can write:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /command

This is certainly no problem, but it is very complicated to write and write. To simplify it, you can write it like this:

0-55/5 * * * * /command

This way of writing has been simplified a lot, and everyone still finds it troublesome, so you can write it like this:

*/5 * * * * /command

This sentence means that the command is executed at 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, and 55 minutes, that is, the command is executed every 5 minutes.

 

Note: There is also a note here (the use of the division sign /):

 

Only divisible step values ​​can express the exact meaning consistent with our understanding, for minutes only */2,*/3,*/4,*/5,*/6,*/10,*/ 12,*/15,*/30, for hours only */2,*/3,*/4,*/6,*/8,*/12.

For dates, if you use */5, it cannot be guaranteed to be executed every 5 days. At the end of the month, it may only be executed after 1, 2 or 3 days, depending on the size of the month and the leap year. This is because cron is stateless, it cannot record the time of the last execution, and can only judge whether it needs to be executed according to the current time.

For the minute bit*/13, it means that within the range of minutes, that is, 0-59 minutes, the task will be executed every 13 minutes.

That is: 0, 13, 26, 39, 52; after another 8 minutes interval, it will re-execute the minute timing interval within a new hour.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326296021&siteId=291194637
Recommended