Detailed explanation of crontab command under Linux

The linux automatic execution tool crontab
is very useful to automatically execute the command crontab
crontab -e to enter the editing mode
shift; or esc                               
q to exit
qw to save and exit
q! Force quit
x delete data
vi edit
59 23 * * 6 /home/weblogic/bin/zxweblogic restart
30 15 * * * /home/weblogic/bin/yyweblogic restart
Basic format:
* * * * * command time-
sharing day-month-week command
The first column indicates minutes 1-59 every minute is indicated by * or */1
The second column indicates hours 1-23 (0 indicates 0 o'clock)
The third column indicates the date 1-31
The fourth column indicates the month 1-12
The fifth column Identification number week 0 to 6 (0 means Sunday) Some examples
of commands to run in column 6 of the
crontab file:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
The above example means 21 every night :30 Restart apache.
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
The above example means restart apache at 4:45 on the 1st, 10th, and 22nd of every month.
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every Saturday and Sunday at 1:10.
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every 30 minutes between 18:00 and 23:00 every day.
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
The above example means to restart apache every Saturday at 11:00 pm.
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
restarts apache every hour
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
11pm Restart apache every hour between 7am and
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
4th of every month and 11am every Monday to Wednesday
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
Restart apache at 4:00 on the 1st of January
Name: crontab
permission: all users
Usage :
crontab file [-u user]-use the specified file replaces the current crontab.
crontab-[-u user]-replace current crontab with standard
input.crontab-1[user]-list user's current
crontab.crontab-e[user]-edit user's current
crontab.crontab-d[user]- Delete the user's current crontab.
crontab-c dir- Specifies the directory of the crontab.
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: Day of the week (0~6, 0 is Sunday).
The program to be run by cmd, the program is sent to sh for execution, this shell only has three environment variables of USER, HOME, SHELL
Description :
Crontab is used to allow users to execute programs at fixed times or at regular intervals, in other words, it is similar to the user's schedule. -u user means to set the schedule of the specified user, the premise is that you must have the authority (for example, root) to be able to specify the schedule of others. If you don't use -u user, it means to set your own schedule.
Parameters:
crontab -e : Execute the text editor to set the schedule, the default text editor is VI, if you want to use another text editor, please set the VISUAL environment variable first to specify which text editor to use (eg setenv VISUAL joe)
crontab -r : delete the current schedule
crontab -l : list the current schedule
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, f3 is the day of the month, f4 is the month, and f5 is the day of the week. program represents the program to be executed.
When f1 is *, it means to execute the program every minute, when f2 is *, it means to execute the program every hour, and so on.
When f1 is ab, it means to execute the program from minute a to minute b, f2 When it is ab, it means that it will be executed from the ath to the bth hour, and the rest is analogous.
When f1 is */n, it means that it is executed every n minutes interval, and f2 is */n, which means that it is executed every n hour interval. Other analogies
When f1 is a, b, c,..., it means a, b, c,... minutes to be executed, and when f2 is a, b, c,..., it means a, b, c... It needs to be executed every hour, and so on.
Users can also store all the settings in the file first, and use the crontab file to set the schedule.
Example:
#Execute /bin/ls every day at 7 am:
0 7 * * * /bin/ls
During 12 months, execute /usr/bin/ every 3 hours from 6 am to 12 am every day backup :
0 6-12/3 * 12 * /usr/bin/backup
5:00 pm every day from Monday to Friday Send a letter to [email protected] :
0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata
every day of the month at midnight 0:20, 2:20, 4:20....do echo "haha"
20 0-23/2 * * * echo "haha "
Note:
When the program is executed at the time you specified, the system will send you a letter showing the content of the program execution. If you do not want to receive such a letter, please add a space after each line > /dev/null 2>&1 is enough
Example 2:




#11pm to 8am every two hours, 8am
0 23-7/2, 8 * * * date #the
4th of every month and every Monday to Wednesday at 11am Point
0 11 4 * mon-wed date #January
day 4:00
4 1 jan * date
example
crontab -l List the user's current crontab.


crontab file
crontab -e Enter edit mode

Set automatically stop at 00:00 every night Start after ten minutes of service
0 0 * * * /home/weblogic/zxweblogic.sh stop
10 0 * * * /home/weblogic/zxweblogic.sh start

Guess you like

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