0

I'm on GoDaddy shared hosting. In my cron job PHP script, I use require() to include some other files. The cron job PHP script works fine when I use it manually (via entering its url to the address bar), but when the cron job is performed, the cron script works fine, except it can't find the required files.

I have the following cron command:

/usr/local/bin/php -q /home/username/public_html/domainname/cron/script.php >/dev/null 2>&1

I read here that one option is to use full paths in require(). Another option is to modify the cron command, which is what I want to learn. My question is how to modify the above cron command so that my require() in the cron script will work (without using full paths).

Thanks.

yenren
  • 450
  • 9
  • 20
  • 1
    `without using full paths` consider making a few constants for your path and including only that file. `define('ROOT_PATH', '....')` etc... You may also be able to simply use `__DIR__` if you are not using it. So It may be helpful to see how you use `require` (tip. no parenthesis needed for it). The only time I had an issue like this with `__DIR__` it was in either a symlink or a bind mount folder (can't remember). But it wasn't PHP's fault (it's like a wormhole in your HD ... lol ) – ArtisticPhoenix Apr 06 '19 at 10:19
  • @ArtisticPhoenix Thanks for your input. Using __DIR__ is a nice option, but still, I would like to learn how to write the correct cron command. – yenren Apr 09 '19 at 05:46
  • For C-Panel that is the correct way, for non-Cpanel you just leave this bit off `/usr/local/bin/` - That said I always use `__DIR__` so I never have these issues. – ArtisticPhoenix Apr 09 '19 at 07:01
  • @ArtisticPhoenix But, as mentioned in my question, when I use the above command, it can't find the required files -files included in the cron script with require()-. – yenren Apr 10 '19 at 06:04

1 Answers1

2

This is probably the easiest way to fix it.

chdir(dirname(__FILE__)); //its been a while but I think __FILE__ works better with bind mounts and symlink but don't quote me on that.
//chdir(__DIR__);

I can't quite remember, but I think I had to use this one time for a wordpress site, that I made a cron job for. If I remember correctly it had some relative directories, from 3rd party plugins etc...

Other wise you can use __DIR__ before your files.

I never use relative directories, I either use a constant

 define('BASE_DIR', __DIR__); //or something else besides __DIR__

Or I just use __DIR__ and never have these kinds of issues. I like knowing exactly where my files are.

require __DIR__.'/somefile.php';
require BASE_DIR.'/somefile.php';

This is not really a PHP issue, its more an issue of how cron calls the PHP file, because the relative paths will be relative to the location of the crontab file.

Summery

That said about using __DIR__ I know from my own experience, you can't control how third parties include files, so chdir is probably the only workable solution in that case.

Hope it helps.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38
  • Thank you for the detailed explanation about using __DIR__. My question about the correct cron command still holds. – yenren Apr 15 '19 at 09:26