Introduced
Cron is a Linux timed execution tool that can run jobs without human intervention.
Under Ubuntu Server, Cron is installed and started by default, and if it is not started, it is set and started (chkconfig\sysv-rc-conf).
The following can be seen through its configuration file/etc/crontab:
Ununtu runs all scripts in four directories on a timed basis by invoking the Run-parts command.
1)/etc/cron.hourly, the script in the directory will be executed once per hour and run at 17 minutes per hour;
2)/etc/cron.daily, the script in the directory will be executed once a day, at 6:25 each day to run;
3)/etc/cron.weekly, the script in the directory will be executed once a week, at 6:47 on the seventh day of the week;
4)/etc/cron.mouthly, the script in the directory will be executed once a month, at the 6:52 of the month of 1th, run;
Of course, the above time is the system default time, can be modified according to their own needs.
Start and stop of cron service
Cron is installed and started by default. While Ubuntu starts, stops and restarts Cron, all by invoking the script in/etc/init.d/. The command is as follows
Start: Sudo/etc/init.d/cron start off: sudo/etc/init.d//etc/init.d/cron Restart reload configuration: sudo/etc/init.d/ | grep cron command to see if Cron is started
You can also use the following command
// Start the service // Close Service // Restart Service // Reload Configuration
To see if Cron is running
Pgrep Cron
Set cron Run log storage path
1. Modify the Rsyslog file
1 sudo vi/etc/rsyslog.d/50-default
Delete the # before the #cron.*;
Restart Rsyslog Service
sudo service rsyslog restart
Restart Cron Service
viewing log files
Tail-f/var/log/cron. Log
Cron file Syntax
1 The syntax is as follows:2 Minute Hour Day Month Dayofweek command3 minutes hours days months days per week order4 each field represents a range of meanings and values as follows:5Minute: Minutes (0-59) that represents the first minute of each hour to perform the task6Hour: Hours (1-23) that represents the hours of the day to perform the task7Day: Date (1-31), which indicates the day of the month to perform the task8Month: Months (1-12), which represents the month ordinal of the year in which the task is performed9DayOfWeek: Week (0-6, 0 for Sunday), indicating the day of the week to perform the taskTen Command : Specify the commands 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 remember to write the full path of the command) OneIn these fields, except that "Command" is the field that must be specified each time, the other fields are optional fields, which can be determined visually. For fields that are not specified, use the "*"to fill its position. At the same time, Cron supports writing similar to regular expressions, and supports several special symbol definitions as follows: A“ *", representing all the numbers within the range of values; -"/", on behalf of "each" ("*/5", representing every 5 units); -"–", representing a number to a number ("1-4", which means 1-4 units); the” ,", separating several discrete numbers; - value range of paragraph meaning -The first paragraph represents the minute 0-59 -The second paragraph represents the hour 0-23 +The third paragraph represents the date 1-31 -The fourth paragraph represents the month 1-12 +The fifth paragraph represents the day of the week, 0 represents Sunday 0-6
Examples are as follows:
// Specify 5 minutes per hour to execute the LS command 5 * * * ls//specify 5:30 to execute LS command per day // specify 7:30 points per month for 8th # execute the LS command 7 * * * Root // every day 7:50 executes all executables in the/etc/cron.daily directory as root
New Cron Task
It is recommended to add a custom task using the CRONTAB-E command (edit the cron file for the user under/var/spool/cron, the crontab file under/var/spool/cron cannot be created directly or modified directly, The crontab file is obtained through the crontab command).
Crontab-e
1) directly execute the command line every 2 minutes to print a string "Hello world", save to file /home/laigw/cron/helloworld. txt, the cron format is as follows: Echo "Hello World" >>/home/helloworld. txt2/home/laigw/cron/test. sh file, the cron format is as follows:/3 * * */home/laigw/cron/test. /home/laigw/cron/test. the contents of SH are as follows: ——————————————————————————————————————— –123
!/bin/sh
cd/home/laigw/cronecho "shell" >> shell. txt ——————————————————————————————————————— –3) php file php file needs to be added command line "#!/usr/local/php /bin/php ", its"/usr/local/php/bin/php "means that you need to introduce the PHP program installation directory of the/bin/php file (the Ubuntu system, the installation directory of the PHP program is/usr/local/php), There are two ways to handle this kind of ingestion file. A. in the PHP file to introduce the command line (recommended), cron format is as follows:/1 * * */home/laigw/cron/test. /home/laigw/cron/test. the contents of PHP are as follows: ——————————————————————————————————————— –1
!/usr/local/php/bin/php
——————————————————————————————————————— – Note: The test . php file needs to be assigned executable permissions: # chmod +x test.php B. /1 * * * */usr/local/php/bin/php/home/laigw/cron/test. php file /home/laigw/cron/test. php are as follows: ——————————————————————————————————————— –
——————————————————————————————————————— – Note: The test. php file needs to begiven executable permissions:# chmod +x test.php5. other /var/spool/cron/log information for all users ' cron service /var/Log/cron logs in this directory 6. crontab 12345 for a super user
Run the ' Atrun ' program every minutes
#This runs anything that's due to run from the ' at '. See mans ' at ' or ' atrun '.
0,5,10,15,20,25,30,35,40,45,50,55 * * * */usr/lib/atrun
7 * * * updatedb
8,10,22,30,39,46,54,58 * * * */bin/sync
7.Example5 * * * Root/sbin/init 6this configures the system to automatically restart every morning at 5 o ' 30. ●0/2 * */sbin/Service httpd Restart means to reboot Apache every two hours .7 * * */sbin/Service sshd start means SSH service is opened every 7:50* * * */sbin/Service sshd Stop means that the SSH service is closed every 22:500 0 1,15 * * fsck/home monthly check 1th and 15th/Home Disk1 * * * */home/bruce/backup the first minute of every hour of execution/home/bruce/Backup this file* * 1-5 find/home ". xxx"-mtime +4-execRM {}/; Every Monday to Friday 3 o'clock, in the directory/home, look for the file name.xxx files and deleted files 4 days ago. ●6/10 * ls means 1, 11, 21 per month., 31st Yes 6:30 execute the LS command once3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/NULL2>&1This statement will be at 3:12 A.M. per day (03:12) Run the tar czf/usr/local/backups/daily/etc.tar.gz/etc command. >>/dev/NULL2>&1 indicates that all standard outputs are sent to the/dev/NULL(Linux Recycle Bin), the standard error output (2) is sent to the same place as the standard output (1) (i.e./dev/NULL). Running this line of command will not produce any output. This statement can become slightly more complex:6 1 * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/NULL2>&1it will be on June 13, Monday: 30 Run Tar czf/usr/local/backups/daily/etc.tar.gz/etc command. The following statements can achieve the same effect:June Mon * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/NULL2>&1If you want to run a program in the 15th minute of every hour as a user, Joey, you can use:* * * * * Joey/usr/bin/somecommand >>/dev/NULL2>&1where the asterisk (*) is a wildcard character, which means that Cron ignores this field. If you want to run a program every two hours, you can use it in the hour field.*/2. It will be at 2, 4, 6 o ... 22 O'Clock, run at 24 o '. The specific statements are as follows:0/2 * * Joey/usr/bin/somecommand >>/dev/NULL2>&1The CRON statement can also use commas (,) to specify multiple times. For example, if you want to run a program at 15 minutes and 30 minutes per hour, you can use 15,30 in the minute field:15,30 * * * * Joey/usr/bin/somecommand >>/dev/NULL2>&1If you want to run a program in the first week of the month (that is, number 1th to 7th) at a specified time of day, you can use the Date field1-7:15,30/2 1-7 * Joey/usr/bin/somecommand >>/dev/NULL2>&1This statement will be in the 1th of the month-7th every two hours of 15 minutes and 30 minutes (02:15,02:30 ... 22:15,22:30, etc.) run/usr/bin/somecommand command. If you want to be in the daily: 18 executes a collection of scripts that can put all the scripts to be executed in a directory (such as/home/username/cron), you can use:* * * * root Run-parts/home/username/cron >>/dev/NULL2>&1if you want to save the output of a program, you can>>/dev/NULL2>&1 replaced by >>
If you use the SH script you also need to modify the Sh script permissions chmod +x/.../.../test.sh
Reference Address: http://blog.csdn.net/LOUISLIAOXH/article/details/48242289
Ubuntu uses Cron to set a timed start task