Home >Backend Development >PHP Tutorial >Detailed explanation on the use of crontab_PHP tutorial

Detailed explanation on the use of crontab_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:04:11917browse

Use crontab to execute PHP code regularly, for example, every ten minutes:
/10 * * * * wget -q --sqider http://******
1. Use PHP to execute scripts in Crontab
Just like calling ordinary shell scripts in Crontab (specific Crontab usage), use PHP program to call PHP scripts.
Execute myscript.php every hour as follows:

Copy the code The code is as follows:

# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php

/usr/local/bin/php is the path of the PHP program.
2. Use URL to execute scripts in Crontab
If your PHP script can be triggered by URL, you can use lynx or curl or wget to configure your Crontab .
The following example uses Lynx text browser to access a URL to execute a PHP script every hour. Lynx text browser uses conversational mode to open URLs by default. However, as shown below, we use the -dump option on the lynx command line to convert the URL output to standard output.
Copy code The code is as follows:

00 * * * * lynx -dump http://www.jb51.net/ myscript.php

The following example uses CURL to access the URL to execute a PHP script every 5 minutes. Curl displays output on standard output by default. You can also dump the script's output to a temporary file using the "curl -o" option.
Copy code The code is as follows:

*/5 * * * * /usr/bin /curl -o temp.txt http://www.jb51.net/myscript.php

The following example uses WGET to access the URL to execute a PHP script every 10 minutes. The -q option indicates quiet mode. "-O temp.txt" means the output will be sent to a temporary file.
Copy code The code is as follows:

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

Here is a detailed introduction:
Name:
crontab
Permissions: All users
Usage:
crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }
Instructions:
crontab is used to allow users to execute programs at fixed times or fixed intervals. In other words, it is similar to the user's schedule. -u user refers to setting the schedule of the specified user. The premise is that you must have its permissions (for example, root) to specify the schedule of others. If -u user is not used, it means setting your own schedule.
Parameters:
crontab -e: Execute a text editor to set the schedule. The default text editor is VI. If you want to use another text editor, please first Set the VISUAL environment variable to specify which text editor to use (for example, 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 schedule format is as follows:
f1 f2 f3 f4 f5 program
where f1 represents minutes, f2 represents hours, f3 represents the day of a month, and f4 represents Month, f5 represents the day of the week. program represents the program to be executed.
When f1 is *, it means the program will be executed every minute, when f2 is *, it means the program will be executed every hour, and so on.
When f1 is a-b, it means the period from minute a to minute b. To be executed within the time, when f2 is a-b, it means that it will be executed from the a-th hour to the b-th hour, and so on
When f1 is */n, it means that it will be executed every n minutes, and f2 is */n. It is executed every n hours, and so on
When f1 is a, b, c,..., it means that it will be executed in the a, b, c,... minute, and f2 is a, b, c ,... means that the a, b, c... hours will be executed, and the rest can be deduced
Users can also store all settings in the file first and use crontab file to set them. schedule.
Example:
#Execute /bin/ls every day at 7 am:
0 7 * * * /bin/ls
In 12 months, every morning at 6 From 12 o'clock to 12 o'clock, /usr/bin/backup is executed every 3 hours:
0 6-12/3 * 12 * /usr/bin/backup
5:00 pm every day from Monday to Friday Send a letter to alex@domain.name:
0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata
0:20 midnight every day of the month , 2:20, 4:20....Execute echo "haha"
20 0-23/2 * * * echo "haha"
Note:
When the program is executed at the time you specify, 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 leave a space after each line. Add > /dev/null 2>&1 to get
Example 2:
#Every day at 6:10 am
10 6 * * * date
#Every two hours
0 */2 * * * date
#Every two hours between 11pm and 8am, 8am
0 23-7/2, 8 * * * date
#The 4th of each month and 11 am from Monday to Wednesday of each week
0 11 4 * mon-wed date
#4 am on January day
0 4 1 jan * date
Example
$crontab -l lists the user's current crontab.
The function of the crontab command is to schedule the execution of some commands at a certain time interval. There is a crontab file in the /etc directory, which stores some schedulers for system operation. Each user can create his or her own scheduling crontab.
The crontab command has three forms of command line structures:
crontab [-u user] [file]
crontab [-u user] [-e|-l|-r]
crontab -l -u [-e|-l|-r] In the first command line, file is the name of the command file. If this file is specified on the command line, then executing the crontab command will copy this file to the crontabs directory; if this file is not specified on the command line, the crontab command will accept commands typed on the standard input (keyboard) and Also store them in the crontab directory.

The -r option in the command line is to delete the user-defined file crontab from the /usr/spool/cron/crontabs directory;
The -l option in the command line is to display the user crontab file content.
Use the command crontab -u user -e to edit the cron(c) job of user user. Users can add or modify any job request by editing the file.

Execute the command crontab -u user -r to delete all cron jobs of the current user.
Jobs and their scheduled times are stored in the file /usr/spool/cron/crontabs/username. username is the username, and the commands to be run by the user are stored in the corresponding file. The results of command execution, whether standard output or error output, will be sent to the user in the form of email. Each request in the file must contain six fields separated by spaces and tabs. The first five fields can take integer values ​​to specify when to start work. The sixth field is a string, called the command field, which includes the command scheduled to be executed by crontab.
The integer value range and meaning of the fifth field in the first line is:
0~59 means minutes
1~23 means hours
1~31 means days
1~12 represents the month
0~6 represents the week (0 represents Sunday)
/usr/lib/cron/cron.allow represents who can use the crontab command. If it is an empty file it indicates that no user can schedule the job. If this file does not exist and there is another file /usr/lib/cron/cron.deny, only users not included in this file can use the crontab command. If it is an empty file, any user can schedule jobs. When both files exist, cron.allow takes precedence. If neither file exists, only the superuser can schedule jobs.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327796.htmlTechArticleUse crontab to execute PHP code regularly, for example, every ten minutes: /10 * * * * wget -q --sqider http://****** 1. Using PHP to execute scripts in Crontab is like calling ordinary scripts in Crontab...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn