Home > Article > Backend Development > Analysis of the reasons for failure of scheduled PHP scheduled tasks using crontab under Linux_PHP Tutorial
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, our Linux regularly executes php code, for example:
*/5 * * * * /usr/local/php/bin/php /home/wwwroot/1.php
In fact, this can execute 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 files included in the executable file, such as:
include_once'./mysql.php';
When a relative path is used in the php code, you can only enter that directory,
Then execute /usr/local/php/bin/php /home/wwwroot/1.php to reference mysql.php to take effect.
The solution is as follows:
*/10 * * * * cd /home/wwwroot && /usr/local/php/bin/php /home/wwwroot/1.php
At the same time, the cache can be generated in /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.