Linux crontab command format

  
 

Basic format: * * * * * Command time-of-day day and month week command

The first column indicates minutes 1 to 59. Use * or */1 for each minute to indicate the second column indicates hours 1 to 23 ( 0 means 0 point) The third column indicates the date 1 to 31. The fourth column indicates the month 1 to 12, the fifth column identification number, the week 0 to 6 (0 means Sunday), the sixth column to run the command

crontab file Some examples:

30 21 * * * /usr/local/etc/rc.d/lighttpd restart The above example means restarting apache every night at 21:30.

45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart The above example shows 4:45 restart apache on 1, 10, 22 every month.

10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart The above example shows restarting apache every Saturday and Sunday at 1:10.

0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart The above example shows that apache is restarted every 30 minutes between 18: 00 and 23: 00 every day.

0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart The above example shows restarting apache every Saturday at 11:00 pm.

* */1 * * * /usr/local/etc/rc.d/lighttpd restart restart apache every hour

* 23-7/1 * * * /usr/local /etc/rc.d/lighttpd restart between 11pm and 7am, restart apache every hour

0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd Restart 4th of every month and 11:00 every Monday to Wednesday restart apache

0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart 4 o'clock on January 1st restart apache< Br>


Name: crontab

Use Permissions: All Users

Usage:

crontab file [-u user]- The specified file replaces the current crontab.

crontab-[-u user]-Replace the current crontab with standard input.

crontab-1[user]-list the user's current crontab.

crontab- e[user]-Edit user's current crontab.

crontab-d[user]-delete the user's current crontab.

crontab-c dir- Specify the crontab directory.

The format of the crontab file: M H D m d cmd.


M: minutes (0-59).

H: Hours (0-23).

D: Day (1-31).

m: Month (1-12).

d: The day of the week (0~6,0 is Sunday).

cmd to run the program, the program is sent to sh execution, this shell only USER, HOME, SHELL three environment variables

Description:

crontab is used Let the user execute the program at a fixed time or at a fixed interval, in other words, a schedule similar to the user. -u user refers to setting the schedule of the specified

user. This premise is that you must have permission (such as root) to be able to specify someone else's schedule. If you do not use -u user, it means setting

to your own schedule.



Parameters:

crontab -e : Execute a text editor to set the schedule, the default text editor is VI, if If you want to use another text editor, please set the VISUAL environment variable

to specify the text editor (eg setenv VISUAL joe)

crontab -r : delete the current one Time schedule

crontab -l : List current schedules

crontab file [-u user]-Replace the current crontab with the specified file.

The format of the schedule is as follows:

f1 f2 f3 f4 f5 program

where f1 is the minute, f2 is the hour, and f3 is the day of the month. , f4 represents the month, and f5 represents the day of the week. Program indicates the program to execute the

line.

When f1 is *, it means that the program is executed every minute. When f2 is *, it means that the program is executed every hour. The other way is to push

when f1 is ab, it means from the a minute. Execute to the time of the bth minute. When f2 is ab, it means to execute from the ath to the bth hour. The other way is to push the

when f1 is */n, it means every n minutes interval. Execute once, f2 is */n means that every n hours interval is executed, and so on.

When f1 is a, b, c,..., it means the a, b, c,.. Minutes to be executed, f2 is a, b, c,... when the first, b, c... hours are to be executed, and so on.

Users can also set all settings. First stored in the file file, use the crontab file to set the schedule.

Example:

#Execute once every 7 am /bin/ls :

0 7 * * * /bin/ls

In December Inside, every day from 6 am to 12 am, /usr/bin/backup is executed every 3 hours:

0 6-12/3 * 12 * /usr/bin/backup

From Monday to Friday, every day at 5:00 pm, send a letter to [email protected] :

0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata

Every day at midnight every day at 0:20, 2:20, 4:20....execute echo "haha"

20 0-23/2 * * * echo "haha"

Note:

When the program is executed at the time you specify, the system will send you a letter showing the contents of the program execution. If you don't want to receive such a message, please add > /dev/null 2>&1 to

after each line.


Example 2:

#6:10 every morning

10 6 * * * date

#every two hours

0 */2 * * * date

#Every two hours between 11pm and 8am, 8am

0 23-7/2,8 * * * date

#each The 4th of the month and the Monday of each week to 11:00 am on Wednesday

0 11 4 * mon-wed date

#1st, 4am,

0 4 1 jan * date

example

$crontab -l lists the user's current crontab.

Copyright © Windows knowledge All Rights Reserved