Home > Article > Backend Development > Analysis of the reasons for the failure of crontab scheduled PHP scheduled tasks under Linux_PHP Tutorial
Analysis of the reasons for the failure of crontab scheduled PHP scheduled tasks under Linux
The reasons for the failure of using crontab to implement scheduled PHP scheduled tasks under Linux.
Many people use crontab to implement PHP to execute scheduled tasks under Linux but fail to generate cache. This article analyzes the reasons for the failure of using crontab to implement scheduled PHP scheduled tasks under Linux.
Generally Linux regularly executes php code, for example:
*/5 * * * * /usr/local/php/bin/php /home/wwwroot/1.php
In fact, this can be executed php code.
But why do many friends write a cache file in 1.php but it is not generated?
This involves the relative path issue of crontab executing php.
Note that there are include files in the execution file, such as:
include_once'./mysql.php';
When using a relative path in the php code, you can only enter that directory, www.jbxue.com
Then execute /usr/local/php/bin/php /home/wwwroot/1.php Only by quoting mysql.php can it take effect.
Solution:
*/10 * * * * cd /home/wwwroot && /usr/local/php/bin/php /home/wwwroot/1.php
At the same time, the cache can be generated in this way /home/wwwroot. Special attention is needed here!
This is something that most of the so-called Linux scheduled execution of PHP has not mentioned, and it is also the most likely to cause misunderstandings.
Of course there are many methods, and you can also set environment variables and the like. This is just a simple method.