The first part crontab introduction
Each operating system has its automatic Timer launcher function, Windows has its Task Scheduler, and Linux corresponds to the function of crontab.
Crontab Introduction
The crontab command is commonly used in Unix-and Unix-like operating systems to set instructions that are executed periodically. The command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word derives from the Greek language Chronos (Χρ?νος), which was originally meant to be time. Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be executed. This type of work is generally called cron jobs.
Crontab usage
The format of the crontab 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 indicates the path to execute.
- 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
Admin login ssh, enter command crontab-e edit crontab file, enter and save according to the above format.
crontab 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
Crontab usage is very easy to master, understand the use of crontab, the site and server maintenance to play a great help, such as scheduled backup, timing optimization server.
Part II Ubuntu How to automatically execute PHP scripts with crontab timing
Now far away from the blog has also encountered the need to run PHP scripts at a specified time, such as regular site data statistics, automatic database backup tasks, scheduled to send email and so on.
If we don't use cron to execute PHP scripts, maybe we'll take the PHP include approach. For example, add code in index.php: <?php include "crontab.php"?>. When a visitor accesses http://farlee.info/index.php through a browser, the crontab.php is executed automatically. Of course, this is only a way of thinking, this method does not specify the time to run PHP scripts, but also to index.php to bring additional server consumption. The Linux Ubuntu Crontab can do a good job of PHP timing, which is also the purpose of writing this article about Cron.
And then the first thought is the same as the scheduled tasks under Windows System, the implementation of a scheduled task by using PHP command-line mode in Windows to automate the execution of PHP scripts automatically, this is a thought, another idea is to access the PHP script directly in the browser address, Automatically open the browser to access the URL, execute PHP script. Therefore, under the Ubuntu Server System, there are similar two kinds of ideas. The following http://farlee.info/steps through the Setup method of using Ubuntu crontab to execute PHP scripts at timed intervals.
PHP installation mode
First, we must first confirm the Linux Ubuntu server in the installation mode of PHP, CGI module or Apache module installation? If you are unsure of the PHP installation mode, you can use the Phpinfo () function to view: <?php phpinfo (),?> write to the Web server in the PHP file, visit the page, at the top of the page we find "server API", if similar " Apache 2.0 Handler ", then the Apache module, otherwise PHP is the CGI mode (Shell command line mode). These two different PHP installation modes are corresponding to the two workarounds for the Ubuntu crontab timed PHP implementation described earlier.
CGI compilation
If the server API is CGI, add a piece of code #!/usr/bin/php-q (similar to Perl syntax) to the first line of the PHP script that requires cron execution, and specify the location of the PHP executable in the server, as follows:
1. Confirm that the Ubuntu system has the PHP5-CLI module installed, install the command: Apt-get install PHP5-CLI. Build the PHP executable under directory/usr/bin/php.
2. Edit the php script file crontab.php to be executed when it is pending, such as:
#!/usr/bin/php-q
<?php
$file = "/var/www/". Date (' H '). ' _ '. Date (' I '). ' _ '. Date (' s '). TXT ';
File_put_contents ($file, date (' y-m-d '));
?>
3. Edit the crontab task:
Open terminal, enter command: CRONTAB-E,
Select an editor. To change later, run ' Select-editor '.
1./bin/ed
2./bin/nano <--Easiest
3./usr/bin/vim.tiny
After selecting an Ubuntu editor, edit the/tmp/crontab. Aun7iq/crontab file:
# m H Dom Mon Dow command
Add a timed execution command below:
* * * * * * php/path/to/your/cron.php
5 asterisks represent the time specified for each minute, hour, day, month, and week, respectively. If the crontab.php is executed 7 o'clock in the afternoon every day, the command is:
* * * * * php/var/www/cron/crontab.php
Then press Ctrl+o to save, ctrl+x exit.
4. Execute the following crontab command line
Shell> crontab crontab
Verify that the crontab.php permissions are executable ("chmod 755/var/www/cron/crontab.php").
Apache Module
If the PHP installation is running in Apache mode, you will not need to #!/usr/bin/php-q this line at the beginning of the PHP script to be executed on the Ubuntu server. PHP scripts are performed in Ubuntu via Crontab's timed execution via the Lynx browser's automatic access.
1. Lynx Browser installation.
The Lynx Browser is a lightweight web browser that is used in most UNIX and Linux system environments. installation command: sudo apt-get install lynx.
2. Edit Cron Task, command crontab-e
* * * * * * lynx-dump http://farlee.info/cron.php
Note This address is best with an absolute address, including the full URL of "http://".
3. Execute the following command line again:
Shell> crontab crontab
Part Three if your PHP script can be triggered by a URL, you can use Lynx or curl or wget to configure your crontab.
The following example uses the Lynx text browser to access the URL to execute PHP scripts every hour. The Lynx text browser opens the URL by default using dialog. However, like the following, we use the-dump option in the Lynx command line to convert the output of the URL to standard output.
1 |
00 * * * * lynx -dump http: //www .centos.bz /myscript .php |
The following example uses Curl to access the URL to execute PHP scripts every 5 minutes. Curl defaults to show output in standard output. With the "curl-o" option, you can also dump the output of the script to a temporary file.
1 |
* /5 * * * * /usr/bin/curl -o temp.txt http: //www .centos.bz /myscript .php |
The following example uses the wget access URL to execute PHP scripts every 10 minutes. The-q option indicates quiet mode. "-O Temp.txt" indicates that the output is sent to a temporary file.
View Source
1 |
* /10 * * * * /usr/bin/wget -q -O temp.txt http: //www .centos.bz /myscript .php
|
Crontab scheduled execution of tasks