3

I wrote a command line utility using Zend Framework to do some nightly reporting. 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. Seems like it should be easily fixed with set_include_path, but maybe I'm missing something?

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:

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. 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:

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

I think PHP is accepting my new path, because when I run it command line and dump the phpinfo I can see:

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

summerg
  • 57
  • 6

2 Answers2

2

Take a look at the Pádraic's approach to zf-cli at ZFPlanet.

Here is a little shell script I use to execute php file from the shell, so I'm sure what the cwd is:

#!/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.

takeshin
  • 49,108
  • 32
  • 120
  • 164
  • thanks, the trick I ended up using was very similar to the ZFPlanet sample you posted. I think the key was the use of: realpath(dirname(__FILE__) . '/../application') everything is now working as planned :) – summerg May 04 '10 at 21:09
0

Most probably the current directory of the CRON job is not the commandline directory. Use getcwd() to check the current directory.

[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 . . You can use realpath() to get the the absolute path, before you add it to the include_path.

Matijs
  • 2,533
  • 20
  • 24
  • correct, I have cron running as me, so it runs from my home directory, /home/summer. I also tried a relitive path from my home directory. set_include_path('../../var/www/clientname/library/' . PATH_SEPARATOR . get_include_path()); Doesn't work. Seams like I should be able to make it absolute from / right? – summerg Apr 27 '10 at 20:21
  • Yeah you should not. I just added that to my answer. – Matijs Apr 28 '10 at 08:31