Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
require_once在命令行中不起作用 - 源码之家

require_once在命令行中不起作用

问题描述:

php /home/test9/public_html/degerlendir/test-4567.php "var1=18&var2=22" 

我需要使用cron作业在后台运行一个页面。我在上面用命令测试了我的代码。但是,我得到这个错误:require_once在命令行中不起作用

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so' - /usr/lib64/extensions/no-debug-non-zts-20100525/imagick.so: cannot open shared object file: No such file or directory in Unknown on line 0 
PHP Warning: require_once(../config.php): failed to open stream: No such file or directory in /home/test9/public_html/degerlendir/test-4567.php on line 2 
PHP Fatal error: require_once(): Failed opening required '../config.php' (include_path='.:/usr/lib64/php') in /home/test9/public_html/degerlendir/test-4567.php on line 2 

问题是页面不包括的config.php在父目录。通常在浏览器中工作的页面。我尝试使用不同的require_once变体,如require_once($ _SERVER ['DOCUMENT_ROOT']。“/ config.php”)。我无法让它工作。

Cron作业始终从您的根目录中获取包含文件的完整路径。

/home/test/.../your-file

在你的cron作业,你需要cd到正确的工作目录,让你的PHP文件,找到它的包含。我这样做是通过创建小的shell脚本,并从cron运行shell脚本:

#!/bin/bash 
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 
cd ${DIR} 
php test-4567.php 

exit 0 

这也给你做一些有用的东西像检查,看看是否该脚本已在运行,以确保您不要的能力如果这是你想要避免的事情,那么你不会启动多个线程。

#!/bin/bash 
FIND_PROC=`ps -ef |grep "php test-4567.php" | awk '{if ($8 !~ /grep/) print $2}'` 
# if FIND_PROC is empty, the process has died; restart it 

if [ -z "${FIND_PROC}" ]; then 
     DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 
     cd ${DIR} 
     php test-4567.php 
fi 

exit 0 

从命令行没有$_SERVER['DOCUMENT_ROOT']。那个只能从http-server(Apache)获得。

工作目录不会自动设置。如果您提示当前位于/ some/path /,脚本将尝试在/some/config.php中查找config.php。

尝试光盘到当前路径通过在脚本的开始使用__DIR__

<?php chdir(__DIR__); ?>