Executing PHP script in crontab (include path problems)

I’m attempting to run a script by launching it daily from the crontab. The script works fine when I call it from the browser. However, whenever the crontab attempts to initiate the script it gets an error.
Warning: require_once(/pathtomyscript/includes/co… failed to open stream: No such file or directory in /pathtomyscript/cron.php

Apparently the crontab has difficulties with executing scripts that contain relative paths. To fix this I tried changing the path to an absolute path like /home/www/public_html/pathtoscript/inclu… however this gives me the error of illegal include. How should I control my include paths so that they work with the cron tab?

The scripts are owned by root and the cronjob is executed by root.

I’m using mysql and the sphider php search engine. The indexing script has some degree of complexity.

Shouldn’t I be able to adjust the include path so that the crontab located in
/etc/crontab
can call the index script in
/home/www/public_html/search/admin/cron_index.php
and the cron script can include a config file at:
/home/www/public_html/search/includes/config.conf

The script does not have to be accessible to anything other than the crontab. It is in the search directory just to keep it in close proximity with all the search elements.

Thanks for your help with understanding why the paths don’t work and what I can do to fix them.

I run a simple cron job and read that if you are running a php file you need to add php to the command so try:


php /home/www/public_html/search/admin/cron_index.php

Yes I execute it as such in my crontab. The problem lies in the include statement in the php script that is being executed. If I leave it as a relative path then the script won’t execute because the crontab doesn’t actually execute from the scripts location so it can’t find the required files. On the other hand if i put a absolute path from the /home/ directory I get an error of “illegal include”. I need to figure out exactly how to handle include paths when being executed via the crontab because Nothing I try seems to work.

Thanks.

Are some of the included files owned by the user who runs the HTTP server (e.g., “nobody”, “apache”, “webuser”)? This may be the case if the files were installed with a PHP or CGI installer.

Ok I think I figured out what I need to do but i’m still trying to interpret this because i’ve never used ini_set before. And all files are owned by the same user including the include files.

This is the information I found:

Actually, there is a way to use includes. I had to do this with the
includes because I had multiple levels of scripts and even needed to run
the pear::Mail library. The main thing to conceptualize is that you’re
not using Apache’s environement anymore so your paths aren’t the same as
if you were coming in from a browser. Nevertheless, you can set your env
(path) on the fly and it will behave nicely again LOL:

// Set path since we’re not using apache env
ini_set(“include_path”,
“.:/usr/lib/php:/usr/local/lib/php:/home/path/to/pear”);

// Now I can include these scripts (which in fact call scripts that call
other scripts…etc.,etc.
include ‘Mine_uses_pear_Mail.php’;//Mail actually includes other
php…so you see how hairy it can get!
include ‘DB.php’;

so for me would I put this in place of my include statments in my php script:
ini_set(“include_path”,".:/usr/lib/php:/usr/local/lib/php:/home/www/public_html/search/includes/
");

include(“config.conf”);

EDIT: This seems to have worked.

I’ve never heard of a php error “illegal include”, so this is probably something generated by your script.

Normally, the current working directory in a php script is set to be the directory of the first script executed in an http request. But not when running php though the command line.

One way around it is to manually set the cwd via chdir()
for example
php /path/to/foo.php


chdir('/home/www/public_html/search/admin/');
include 'cron_index.php';

You might be able to use include_path() instead, or might need to. It depends on how your script was written.

You might need to dig into your script and find the true meaning behind “illegal include” before a solution becomes clear.

As your script is under your www root (and assuming it works from there) you could add a
wget http://www.example.com/search/admin/cron_index.php
to your cron