Crontab using _php Tutorials

Source: Internet
Author: User
Use Crontab to execute PHP code at regular intervals, with an example of every 10 minutes:
/10 * * * * wget-q--sqider http://******

First, use PHP to execute script in crontab

Just like calling a normal shell script in crontab (specific crontab usage), use a PHP program to invoke the PHP script.
Each hour executes myscript.php as follows:
Copy the code code as follows:

# CRONTAB-E
XX * * * */usr/local/bin/php/home/john/myscript.php

/usr/local/bin/php is the path to the PHP program.

Second, use URL to execute script in crontab

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.
Copy the code code as follows:

XX * * * * lynx-dump http://www.jb51.net/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.
Copy the code code as follows:

*/5 * * * */usr/bin/curl-o temp.txt http://www.jb51.net/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.
Copy the code code as follows:

*/10 * * * */usr/bin/wget-q-o temp.txt http://www.jb51.net/myscript.php


Here's a detailed introduction to him:
Name: crontab
Usage rights: All users
How to use:
crontab [-u user] File
crontab [-u user] {-l |-r |-e}
Description
Crontab is used to allow a user to execute a program at a fixed time or at a fixed interval, in other words, a user-like schedule. -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:
CRONTAB-E: Do 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)
Crontab-r: Delete the current schedule table
Crontab-l: List the current schedule
crontab file [-u user]-replaces the current crontab with the specified files.
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 procedure to be executed.
When F1 is * indicates that every minute the program,f2 is executed, the program is executed every hour, and the remainder 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 to the first, and the remainder of the analogy
When F1 is */n, it is performed once every n minutes, and F2 is */n for every n-hour interval, with the remainder of the other 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.
Example:
#每天早上7点执行一次/bin/ls:
0 7 * * */BIN/LS
In December, the/usr/bin/backup is performed every 3 hours in the morning from 6 to 12.
0 6-12/3 */usr/bin/backup
Monday to Friday every 5:00 send a letter to Alex@domain.name:
0 * * 1-5 mail-s "HI" Alex@domain.name </tmp/maildata
Every day of the month 0:20 midnight, 2:20, 4:20 .... Perform echo "haha"
0-23/2 * * * echo "haha"
Attention:
When the program is executed at the time you specify, the system will send you a letter showing what the program is doing, and if you do not wish to receive such a letter, please add >/dev/null 2>&1 after each line is empty.
Example 2:
#每天早上6点10分
6 * * * Date
#每两个小时
0 */2 * * * Date
#晚上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
Example
$crontab-L lists the user's current crontab.
The function of the crontab command is to schedule execution of some commands at certain intervals. There is a crontab file in the/etc directory, where there are some scheduler programs running on the system. Each user can establish their own dispatch crontab.

The crontab command has three forms of command-line structure:

crontab [-u user] [file]

crontab [-u user] [-e|-l|-r]

Crontab-l-U [-e|-l|-r] the first command line, file is the name of the command files. If you specify this file on the command line, then execute the crontab command, copy the file to the Crontabs directory, or if the file is not set at the command line, the crontab command will accept the command typed on the standard input (keyboard). They are also stored in the crontab directory.

The function of the-r option on the command line is to remove the user-defined file crontab from the/usr/spool/cron/crontabs directory;

The function of the-l option in the command line is to display the contents of the user's crontab file.

Use the command crontab-u user-e command to edit the cron (c) job for user users. The user edits the file to add or modify any job request.

Execute command crontab-u user-r to delete all cron jobs for the current user.

The jobs and their scheduled time are stored in the file/usr/spool/cron/crontabs/username. Username uses the username to store the commands that the user wants to run in the appropriate file. The result of the command execution, either standard output or error output, is sent to the user in the form of a message. Each request in the file must contain six domains separated by spaces and tabs. The first five fields can take an integer value, specify when to start working, and the sixth field is a string, called a command field, which includes the command that the Crontab dispatch executes.

The integer value range and the meaning of the first fifth field are:

0~59 represents the Division

1~23 = Hour

1~31 represents the day

1~12 represents the Month

0~6 represents the week (where 0 represents Sunday)

/usr/lib/cron/cron.allow indicates who can use the crontab command. If it is an empty file indicates that no one user can schedule the job. If the file does not exist and there is another file/usr/lib/cron/cron.deny, only the user who is not included in the file can use the crontab command. If it is an empty file, it indicates that any user can schedule the job. Two files exist at the same time Cron.allow priority, if none exist, only the Superuser can schedule the job.


Excerpt from Andy1219111 's column

http://www.bkjia.com/PHPjc/478231.html www.bkjia.com true http://www.bkjia.com/PHPjc/478231.html techarticle Execute PHP code with crontab Timing, example every 10 minutes:/10 * * * * wget-q--sqider http://****** One, use PHP in crontab to execute script like in crontab ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.