很多人在linux下使用crontab实现PHP执行定时任务却未能成功,不能生成缓存。本文就linux下使用crontab实现定时PHP计划任务失败的原因做一分析。
一般我们linux定时执行php代码例如:
*/5 * * * * /usr/local/php/bin/php /home/wwwroot/1.php
其实这样是可以执行php代码的。
但是为什么很多朋友在1.php写的生成一个缓存文件没有生成呢?
这个要涉及到crontab执行php的相对路径问题.
注意在执行文件中,有包含文件的,如:
include_once'./mysql.php';
当php代码中,用的是相对路径时,只有进入到那个目录下,
然后执行/usr/local/php/bin/php /home/wwwroot/1.php 才能引用mysql.php生效。
解决方法如下:
*/10 * * * * cd /home/wwwroot && /usr/local/php/bin/php /home/wwwroot/1.php
同时也这样缓存才能生成在 /home/wwwroot。此处需要特别注意!
这是大部分写的所谓linux定时执行php没有提到的,也最容易产生误区的。
当然方法还有很多,还可以设置环境变量之类的。这个只是一个简单的方法。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor