简体   繁体   English

CLI包括通过cron运行zend框架的路径

[英]CLI include paths to run zend framework via cron

I wrote a command line utility using Zend Framework to do some nightly reporting. 我使用Zend Framework编写了一个命令行实用程序来进行一些夜间报告。 It uses a ton of the same functionality the accompanying site. 它在附带的网站上使用了大量相同的功能。 It works great when I run it by hand, but when I run it on cron I have include path issues. 当我手动运行它时效果很好,但是当我在cron上运行它时,我会包含路径问题。 Seems like it should be easily fixed with set_include_path, but maybe I'm missing something? 好像它应该很容易用set_include_path修复,但也许我错过了什么?

My directory structure looks like this: 我的目录结构如下所示:

/var/www/clientname/
    application
        Globals.php
    commandline
        commandline_bootstrap.php       
    public_html
        public_bootstrap.php        
    library
        Zend

In public_bootstrap.php I use set_include_path without a problem, relative to the current directory: 在public_bootstrap.php中,相对于当前目录,我使用set_include_path没有问题:

set_include_path('../library' . PATH_SEPARATOR . get_include_path());  

If I understand correctly, in commandline_bootstrap.php I need to put in the absolute path, so cron knows where everything is. 如果我理解正确,在commandline_bootstrap.php中我需要放入绝对路径,所以cron知道一切都在哪里。 My file starts like this: 我的文件开头是这样的:

error_reporting(E_ALL);
set_include_path('/var/www/clientname/library' . PATH_SEPARATOR . get_include_path());  
require_once "../application/Globals.php";

But when I run it via cron I get the following error: 但是当我通过cron运行它时,我得到以下错误:

PHP Fatal error: require_once(): Failed opening required '../application/Globals.php' (include_path='/var/www/clientname/library/') in /var/www/clientname/commandline/zfcli.php on line 11 PHP致命错误:require_once():在/var/www/clientname/commandline/zfcli.php中打开所需的'../application/Globals.php'(include_path ='/ var / www / clientname / library /')失败第11行

I think PHP is accepting my new path, because when I run it command line and dump the phpinfo I can see: 我认为PHP正在接受我的新路径,因为当我运行命令行并转储phpinfo时,我可以看到:

include_path => /var/www/clientname/library/:.:/usr/share/pear:/usr/share/php => .:/usr/share/pear:/usr/share/php include_path => /var/www/clientname/library/:.:/usr/share/pear:/usr/share/php =>。:/ usr / share / pear:/ usr / share / php

I admit the syntax here looks a little strange, but I can't figure out how to fix it. 我承认这里的语法看起来有点奇怪,但我无法弄清楚如何修复它。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。

Thanks summer 谢谢夏天

Take a look at the Pádraic's approach to zf-cli at ZFPlanet . 看看在帕德里克的方法zf-cliZFPlanet

Here is a little shell script I use to execute php file from the shell, so I'm sure what the cwd is: 这是我用来从shell执行php文件的一个小shell脚本,所以我确定cwd是什么:

#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));
include('doctrine-cli.php');

There was also a bug in the autoloader's isReadable() prior to 1.10.4, try upgrading. 在1.10.4之前, 自动加载器的isReadable()中也存在一个错误 ,请尝试升级。

Most probably the current directory of the CRON job is not the commandline directory. 很可能CRON作业的当前目录不是commandline目录。 Use getcwd() to check the current directory. 使用getcwd()检查当前目录。

[Edit:] [编辑:]

Also do not use relative paths in set_include_path as this may result in unexpected behaviour - except of course for the current directory . 也不要在set_include_path使用相对路径,因为这可能会导致意外行为 - 当然除了当前目录. . You can use realpath() to get the the absolute path, before you add it to the include_path . 在将其添加到include_path之前,可以使用realpath()来获取绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM