Our PHP program sometimes needs timed execution. We can use the ignore_user_abort function or place js on the page to help us implement it. However, both methods are unreliable and unstable. We can use the Linux Crontab tool to trigger PHP Execution tasks stably and reliably. The following describes two methods of Crontab. Common commands: crontab-l
Our PHP program sometimes needs timed execution. We can use the ignore_user_abort function or place js on the page to help us implement it. However, both methods are unreliable and unstable. We can use the Linux Crontab tool to trigger PHP Execution tasks stably and reliably. The following describes two methods of Crontab. Common commands: crontab-l
Our PHP program sometimes needs timed execution. We can use the ignore_user_abort function or place js on the page to help us implement it. However, both methods are unreliable and unstable. We can use the Linux Crontab tool to trigger PHP Execution tasks stably and reliably.
The following describes two methods of Crontab.
Common commands
Crontab-l view
Crontab-e EDIT: wq or: q exit
Crontab-r Delete
Restart service crond restart
Two examples of writing
*/2 *** wget http://mobi.xxxxx.cn/pm2.5/index.php? A = getCity
*/2 ***** echo 'Welcome here. '>/hd.txt
How to check whether a scheduled task is executed:
You can use tail-f/var/spool/mail/root to view the latest crontab execution logs.
View the/var/log/cron file. You can use tail-f/var/log/cron to observe it.
1. Use PHP to execute scripts in Crontab
Just like calling a common shell script in Crontab (specific Crontab usage), you can use a PHP program to call the PHP script.
Execute myscript. php every hour 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 the URL in Crontab to execute the script
If your PHP script can be triggered through a URL, you can use lynx, curl, or wget to configure your Crontab.
The following example uses the Lynx text browser to access the URL and execute the PHP script hourly. By default, the Lynx text browser opens a URL in dialog mode. However, as shown below, we use the-dump option in the lynx command line to convert URL output to standard output.
- 00 *** lynx-dump http://www.centos.bz/myscript.php
The following example uses CURL to access the URL and execute the PHP script every 5 minutes. Curl displays the output in standard output by default. With the "curl-o" option, you can also dump the Script output to a temporary file.
- */5 */usr/bin/curl-o temp.txt http://www.centos.bz/myscript.php
The following example uses the wget url to execute the PHP script every 10 minutes. -Q indicates quiet mode. "-O temp.txt" indicates that the output will be sent to a temporary file.
- */10 ***/usr/bin/wget-q-O temp.txt http://www.centos.bz/myscript.php