Cause analysis of failure of timed PHP scheduling task using crontab under linux

  • 2021-07-06 10:33:43
  • OfStack

Many people use crontab under linux to implement PHP to perform timing tasks, but they fail to generate caches. In this paper, the reasons for the failure of PHP scheduling task using crontab under linux are analyzed.

1. Our linux executes php code at regular intervals, such as:


*/5 * * * * /usr/local/php/bin/php /home/wwwroot/1.php

In fact, php code can be executed in this way.

But why did many friends write 1. php to generate 1 cache file without generating it?

This involves the relative path of crontab to php.

Note that in the execution file, there are files containing files, such as:


include_once'./mysql.php'; 

In the php code, when the relative path is used, the only way to enter that directory is.

The reference to mysql. php can then take effect by executing/usr/local/php/bin/php/home/wwwroot/1. php.

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 at/home/wwwroot. Special attention is needed here!

This is mostly written so-called linux timing execution php not mentioned, but also the most prone to misunderstanding.

Of course, there are many methods, and you can also set environment variables and so on. This is just a simple method.


Related articles: