sudo crontab-e
5 * * * * 5 minutes per hour
*/5 * * * * * every 5 minutes
0 2 * * * Daily 2 o'clock in the morning execution
Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways:
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
You can also start the service automatically when the system starts:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start
Now cron This service is already in the process, we can use this service, Cron service provides the following kinds of interfaces for everyone to use:
1. Edit directly with crontab command
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command
CRONTAB-L//list details of a user cron service
Crontab-r//Delete a user's cron service
CRONTAB-E//Edit a user's cron service
For example, root to view your cron settings: Crontab-u root-l
Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing the Cron service, the edited content has some formatting and conventions, input: Crontab-u root-e
Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
The first part of this format is the time setting, the next part is the command to execute, if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, you can recall the full path of the command when the call. Time setting we have a certain agreement, the preceding five * number represents five numbers, the value range and meaning of the numbers are as follows:
Minutes (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Week (0-6)//0 on behalf of Sunday
In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem:
Every morning at 6.
0 6 * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, no output is visible from the screen, because cron emails any output to root.
Every two hours
0 */2 * * echo "has a break now." >>/tmp/test.txt
Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
0 23-7/2,8 * * * echo "a good Dream:)" >>/tmp/test.txt
Every month, number 4th and Monday to Sunday, three a.m., 11.
0 4 * 1-3 command line
January 1 morning, 4.
0 4 1 1 * command line
After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and can only be edited with CRONTAB-E. After Cron starts, read the file once every one of the clocks, and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.
2. Edit/etc/crontab File Configuration cron
Cron service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
home=///user-run path, here is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly
Attention to the "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.
--------------------------------------
Basic format:
* * * * * command
Time-sharing Weekly command
The 1th column represents minutes 1~59 per minute with * or */1
The 2nd column represents the hour 1~23 (0 means 0 points)
The 3rd column represents the date 1~31
The 4th column represents the month 1~12
5th Column Identification Number Week 0~6 (0 = Sunday)
6th List of commands to run
Some examples of crontab files:
* * * * */usr/local/etc/rc.d/lighttpd restart
The above example shows a 21:30 restart lighttpd per night.
4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows a 4:45 restart lighttpd per month for 1, 10, 22nd.
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows a 1:10 restart lighttpd per Saturday and Sunday.
0,30 18-23 * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that LIGHTTPD restarts every 30 minutes from 18:00 to 23:00 every day.
0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that the LIGHTTPD is restarted every Saturday.
* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart LIGHTTPD every hour
* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart lighttpd every hour.
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month and 11-point restart lighttpd from Monday to Wednesday
0 4 1 Jan */usr/local/etc/rc.d/lighttpd restart
January 1 4-point restart LIGHTTPD
=========================================
Crontab-e
Enter a VI editing interface
In the last line add
*/30 * * * * netstat >/tmp/net.log
Indicates that the netstat command is executed every 30 minutes and the execution results are stored in Net.log.
Crontab is a handy program to perform a task on a unix/linux system on a timed (cyclic) basis.
Using the Cron service, view the Cron service status with service Crond status, and if not, service Crond start starts it,
The cron service is a timed execution service that can be added or edited with the crontab command to perform tasks that need to be timed:
Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command
CRONTAB-L//list details of a user cron service
Crontab-r//Delete a cron service with no users
CRONTAB-E//Edit a user's cron service
For example, root to view your cron settings: Crontab-u root-l
Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing the Cron service, the edited content has some formatting and conventions, input: Crontab-u root-e
Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
Edit the/etc/crontab file and add a line at the end: 5 * * * Root init 6 This configures the system to automatically restart every morning at 5 o ' 30.
You need to set Crond as a service that starts automatically after system startup, and in/etc/rc.d/rc.local, at the end, add
Service Crond Start
If you also need to load additional services on the system boot Ten, you can continue to add the start command for other services.
For example: Service mysqld start
Basic usage:
1. Crontab-l
List the current crontab tasks
2. crontab-d
Delete the current crontab task
3. CRONTAB-E (solaris5.8 above is crontab-r)
Edit a crontab task, Ctrl_d end
4. crontab filename
A task list file that is crontab as filename and loaded into
Format of crontab file:
The rows in the crontab file consist of 6 fields separated by a space or TAB key between the different fields. First 5 fields specify when the command will run
Minutes (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Day of the week (0-6, of which 0 stands for Sunday)
The 6th field is a string to be executed at the appropriate time
Example:
#MIN HOUR Day MONTH DAYOFWEEK COMMAND
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * DATE (Solaris 5.8 does not seem to support this type of notation)
#晚上11点到早上8点之间每两个小时, 8 in the morning.
0 23-7/2,8 * * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 4 * mon-wed date
#1月份日早上4点
0 4 1 Jan * Date
Add: When using crontab, it is important to note that the environment variables that are accessible in the run script are not necessarily consistent with the environment variables in the current test environment, and it is safer to set the environment variables yourself in a running script (export)
(1) First build a file Crond.txt as follows, restart 5:36 A.M. every day
5 * * * reboot
(2) Upload to/opt directory
(3) Run the command
Crontab/opt/crond.txt
Crontab-l
Let the configuration file take effect: If you let the configuration file take effect, you also have to restart Cron, remember, since each user under the Cron configuration file after modification. Also restart the cron server.
In Fedora and redhat, we should use;
[Email protected] ~]#/etc/init.d/crond restart
If you let Crond run at boot time, you should change its operating level;
[Email protected] ~]# chkconfig--levels Crond on
Service Crond Status View Cron service state, if not started, service Crond start it, cron service is a scheduled service, you can add or edit tasks that need to be performed by Crontab command
Each line of the crontab file consists of six fields (minutes, hours, day of month, month, day of the Week, command), separated by spaces or tabs, where:
Minutes: Minute field, the range of values is 0 to 59
Hours: Hour field, the range of values is 0 to 23
Day of Month: date, the range of values is 1 to 31
Month: The range of values is 1 to 12
Day of week: Week, value range is 0 to 6, Sunday value is 0
Command: Commands to run
If a field is *, it indicates that the command can be executed within the range of all possible values for that domain.
Such as??? A field is a two-digit number separated by hyphens, indicating that the command can be executed within the range of two digits (including the two numbers themselves).
If a field is made up of a series of values separated by commas, the command can be executed within the range of these values.
Both fields are valid if the date and weekday fields have values.
Write a file to start the automatic backup process.
Cd/opt
Touch Reboot.txt
Add something to the Reboot.txt:
0 4 * * * reboot
Crontab/opt/reboot.txt
Edit timed operations with CRONTAB-E, such as by adding a downstream command:
Use the crontab-l command to view
Note: The service needs to be started (added in rc.local)
Restart Crond Task
/etc/init.d/cron restart (under Ubuntu)
The first in a system that is managed in RPM packages, such as Fedora or redhat;
[[email protected] ~]#/etc/init.d/crond start
[[email protected] ~]#/etc/init.d/crond stop
[Email protected] ~]#/etc/init.d/crond restart
/etc/rc.d/init.d/crond restart
Introduction to Commands
The crontab-operates the daemon for each user and the schedule for that execution.
Partial parameter description
crontab file [-u user]-replaces the current crontab with the specified files.
Crontab-[-u user]-replaces the current crontab with standard input.
crontab-1[user]-lists 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-Specifies the directory for 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).
CMD to run the program, the program is fed into sh execution, this shell only user,home,shell these three environment variables.
Here is an example file:
#MIN HOUR Day MONTH DAYOFWEEK COMMAND
#每天早上6点
106* * * Date
#每两个小时
0*/2* * * Date
#晚上11点到早上8点之间每两个小时, Early Upper point
0 23-7/2,8* * * Date
#每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
0 mon-wed Date
#1月份日早上4点
0 4 1 jan* date
Example
LARK:~>CRONTAB-1 lists the user's current crontab.
#MIN HOUR Day MONTH DAYOFWEEK COMMAND
Ten 6* * * Date
0*/2* * * Date
0 23-7/2,8 * * * Date
Lark:~>
Usage rights: Root user and owner of crontab file
Grammar:
crontab [-E [Username]|-l [Username]|-r [username]|-v] [username]| File]
Restart: Sudo/etc/init.d/cron restart
Description
Crontab is used to allow a user to execute a program at a fixed or fixed interval, in other words, a user-like time table. -u user refers to the setting of the time table for the specified user, which presupposes that you must have permission (for example, root) to specify another's time schedule. If you do not use the-u user, it means setting your own schedule.
Parameters:
-E [UserName]: Perform a text editor to set the time table, the default text editor is VI, if you want to use a different text editor, you first set the VISUAL environment variables to specify the use of the text editor (for example, setenv visual Joe)
-R [UserName]: Delete current Schedule table
-L [UserName]: Lists the current schedule table
-V [UserName]: Lists the status of the user cron job
The format of the schedule table is as follows:
F1 F2 F3 f4 f5 program
Where F1 is expressed in minutes, F2 represents hours, F3 represents the day of the month, F4 represents the month, and F5 represents the day of the one week. Program represents the command to execute, which is written at the terminal.
When F1 is * indicates that every minute the program,f2 is executed, the program is executed every hour, and the rest of the analogy
When the F1 is a-B, it is executed from the time of the minute to the minute of the hour, and the F2 is a-B indicating that it is executed from the first and the other, and the other analogy
When F1 is */n, it is executed once every n minutes, F2 is */n for every n-hour interval, and the rest of the analogy
When F1 is a, B, C,... A, B, C,... Minutes to execute, F2 for a, B, C,... The time indicated that the first, B, c ... Hours to execute, and the rest of the analogy
The user can also store all the settings in the file file, using the crontab file to set the time schedule.
Because UNIX versions are different, there are some differences in syntax, such as setting interval execution in HP UNIX AIX If syntax error occurs in */n mode, the interval execution can only be enumerated in this type of UNIX, see examples in detail.
How to use:
Edit a file Cronfile with VI and enter a well-formed time table in this file. When the edits are complete, save and exit.
At the command line, enter
$: crontab cronfile
This submits the Cronfile file to the C R o N process, and a copy of the newly created cronfile has been placed in the/V a r/s P o o l/c R o n directory, and the file name is the user name.
Example:
The/bin/ls is performed at the first 0 minutes of every day per month:
0 * * * */BIN/LS
In December, the/usr/bin/backup is performed every 20 minutes in the morning from 6 to 12 hours per day:
*/20 6-12 */usr/bin/backup
Monday to Friday every 5:00 send a letter to [email protected]:
0 * * 1-5 mail-s "HI" [email protected] </tmp/maildata
Every day of the month 0:20 midnight, 2:20, 4:20 .... Perform echo "haha"
0-23/2 * * * echo "haha"
Every two hours between 11 o'clock and 8 in the morning, 8 in the morning.
0 23-7/2,8 * * * Date
In HP Unix, every 20 minutes, it is represented as: 0,20,40 * * * * * instead of using */n mode, or syntax error occurs
Transferred from: http://www.linuxidc.com/Linux/2012-02/54391.htm
Crontab Time Rule