Home > Article > Backend Development > [Transfer] PHP scheduled tasks: How to use Linux's Crontab to execute PHP scripts_PHP tutorial
Redirect: https://www.centos.bz/2011/03/auto-run-task-crontab/
Our PHP program sometimes needs to be executed regularly. We can use the ignore_user_abort function or place js on the page to let the user help us implement it. But both methods are unreliable and unstable. We can use the Linux Crontab tool to trigger PHP execution tasks stably and reliably.
The two methods of Crontab are introduced below.
Use a PHP program to call a PHP script just like calling a normal shell script in Crontab.
Execute myscript.php every hour as follows:
/usr/local/bin/php is the path of the PHP program.
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.
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.
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.