Reputation: 11188
I have set up a cronjob which updates a bunch of contracts in a certain system. When I run the PHP-script in a browser it all works fine, but when the cronjob has to do the trick it fails. I'm kinda stuck on this one since I don't have a lot of experience with cronjobs (heck.. I can only set them up in DirectAdmin).
My PHP scripts has some includes to some classes, these includes work properly (i've tested it by sending mails to myself line by line). When the base-classes are included I have a class which handles autoloading. When I do something like Class::GetInstance() it fails.
My cronjob looks like:
* * * * * /usr/local/bin/php /home/username/domains/domain/public_html/path/to/script.php
What can I do to fix this? Perhaps not run it via php, but by a browser or something? I'm sorry if this is a stupid question, but I don't know this ;)
Upvotes: 0
Views: 189
Reputation: 157863
"it fails" is not the problem description one can call a suffucient one.
add this line in your crontab file
MAILTO=your@mail
and run your jobs.
You will get the script output and be able either to correct your code or ask a sensible question.
You may also redirect stdout and stderr to a log file
Upvotes: 0
Reputation: 17010
Remeber that when PHP is executed on CLI with /usr/local/bin/php
you do not have the $_SERVER
variable setted properly! I had that problem too because my script had to use $_SERVER['DOCUMENT_ROOT']
. As said, try to run it in a normal shell to see if it works. Alternatively you can change your cronjob command to:
wget -q http://yourdomain.com/path/to/script.php
Usually this works well because it is just identical to fetch that URL from a normal browser.
wget
man page here: http://linux.die.net/man/1/wget
Upvotes: 2
Reputation: 30167
You can't always call the php file directly that expects to be called via HTTP. Judging from path, it's a part of website, which is normally executed by browser, hence I'ld set the cronjob up to not to be directly called by php-cli, but rather by doing a curl
request to the website's URL.
Upvotes: 0