The two methods of Crontab are introduced below.
1. Use PHP to execute scripts in Crontab
Just like calling ordinary shell scripts in Crontab (specific Crontab usage), use the 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 example below is to use Lynx text browser to access the 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 The code is as follows:
00 * * * * lynx -dump http://www.jb51.net/myscript.php
The following example is to use CURL to access the URL every 5 minutes Execute PHP script. 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 The code is as follows:
*/5 * * * * /usr/bin/curl -o temp.txt http://www.jb51.net/myscript.php
The following An example is using 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 The code is as follows:
*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.jb51.net/myscript.php
The above introduces the crontab format and how to use Linux Crontab to regularly execute PHP scripts, including the crontab format. I hope it will be helpful to friends who are interested in PHP tutorials.