LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Executing PHP script in crontab (include path problems) (https://www.linuxquestions.org/questions/linux-newbie-8/executing-php-script-in-crontab-include-path-problems-777241/)

bar338 12-21-2009 10:18 AM

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.

Thanks for your help.

Tinkster 12-21-2009 01:12 PM

When you say: "call it from the browser" ... is it actually meant to
be run under the control of apache? If so, why don't you point a
command-line browser like lynx, links or even curl or wget to it
from your cron-job?
Code:

13 17 * * * /usr/bin/lynx http://localhost/pathtomy/cron.php

Cheers,
Tink

bar338 12-21-2009 01:31 PM

Is this secure? Eventually I had planned on either moving the script to a non web-accessible location or password protecting it. The purpose of the script is to initiate a reindex of my database for my search engine.

Tinkster 12-21-2009 01:48 PM

As secure as your server/script.

Which RDBMS are you using, how complex is the PHP script?


Cheers
Tink

bar338 12-21-2009 02:11 PM

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.

Tinkster 12-21-2009 02:36 PM

My knowledge of PHP is quite limited, but my guess is that the paths
in the web-based interface are relative to the document root of the
site - which is why I suggested to access it via a browser. If you
modify the include directives within the php-scripts you may break
the behaviour of the web part. All this, of course, is just general
hand-waving, derived from my ideas on how things work, and why they
don't work from cron. I don't think that any include path you throw
at cron will fix the interdepencies of the scripts.


Cheers,
Tink


All times are GMT -5. The time now is 06:05 PM.