Home > Backend Development > PHP Tutorial > Detailed explanation of crontab usage_PHP tutorial

Detailed explanation of crontab usage_PHP tutorial

WBOY
Release: 2016-07-13 17:51:02
Original
949 people have browsed it

Use crontab to execute PHP code regularly, for example, execute it every ten minutes:
/10 * * * * wget -q --sqider http://******

1. Use PHP to execute scripts in Crontab

Just like calling a normal shell script in Crontab (specific Crontab usage), use the PHP program to call the PHP script.
Execute myscript.php every hour as follows:
Copy the code 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 script in Crontab

If your PHP script can be triggered by URL, you can use lynx or curl or wget to configure your Crontab.
The example below uses a 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 the code 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 the code 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 the code as follows:

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


Here is a detailed introduction to him:
Name: crontab
Access: All users
How to use:
crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }
Description:
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 Say 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 format of the schedule is as follows:
f1 f2 f3 f4 f5 program
Where f1 represents minutes, f2 represents hours, f3 represents the day of a month, f4 represents the month, and 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 that it will be executed from the a-th minute to the b-th minute. 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. When f2 is */n, it means it will be executed every n hours. The rest can be deduced
When f1 is a, b, c,..., it means that the a, b, c,... minute will be executed. When f2 is a, b, c,..., it means the a, b, c... hours to be executed, and so on for the rest
Users can also store all settings in a file first and use crontab file to set the schedule.
Example:
#Execute once every day at 7 am /bin/ls :
0 7 * * * /bin/ls
Within 12 months, /usr/bin/backup will be executed every 3 hours from 6 am to 12 am every day:
0 6-12/3 * 12 * /usr/bin/backup
Send a letter to alex@domain.name every day from Monday to Friday at 5:00 pm:
0 17 * * 1-5 mail -s "hi" alex@domain.name < /tmp/maildata
Execute echo "haha"
at midnight every day of the month at 0:20, 2:20, 4:20.... 20 0-23/2 * * * echo "haha"
Note:
When the program is executed at the time you specified, the system will send you a letter showing the execution content of the program. If you do not want to receive such a letter, please add > / after a space in each line. dev/null 2>&1 is enough
Example 2:
#everyday6:10am
10 6 * * * date
#everytwohours
0 */2 * * * date
#Every two hours between 11pm and 8am, 8am
0 23-7/2, 8 * * * date
#On the 4th of every month and every Monday to Wednesday at 11am
0 11 4 * mon-wed date
#1月日4am
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 certain intervals. 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 function of 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 used to display the contents of the user's crontab file.

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 points

1~23 indicates hours

1~31 indicates day

1~12 represents the month

0~6 means day of the week (0 means Sunday)

/usr/lib/cron/cron.allow indicates 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.


Excerpted from andy1219111’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478231.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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template