2

I am trying to run a php file via cron job, the file works fine when I run it manually, but when I run it in a cron job, I get this error:

Warning: include(classes/EmailAddressValidator.php): failed to open stream: No such file or directory in /var/www/onecent_dev/classes/MiscFunctions.php on line 3

Warning: include(): Failed opening 'classes/EmailAddressValidator.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/onecent_dev/classes/MiscFunctions.php on line 3

MiscFunctions.php & EmailAddressValidator.php are both existing files and are in the right place, what gives?

Thanks

user979331
  • 11,039
  • 73
  • 223
  • 418

2 Answers2

11

Looks like your include_path is resolving . to whatever cron happens to have the current directory set to, as opposed to the directory where your script is. Try editing your crontab to cd first:

0 * * * * cd /path/to/script && php script.php

Or provide the include_path explicitly:

0 * * * * php -d include_path=/path/to/script script.php
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • Using the `include_path` method did not work for me. I got "Could not open input file: script.php" The first option worked and seems simple enough. My question is, is there any con to `cd`ing in a Cron job? Cron gets its own shell and does whatever there, right? Would this mean future jobs would technically be run in /path/to/script then? Where is Cron's default working directory then? Thanks – user2402616 Oct 18 '21 at 18:49
  • 1
    _"Would this mean future jobs would technically be run in /path/to/script then?"_ No, just this job. You could also do `php /path/to/script.php` without the cd first. – Alex Howansky Oct 18 '21 at 18:58
0

See this question: PHP: Require path does not work for cron job?

Your include_path doesn't contain the path of the script you're executing.

Community
  • 1
  • 1
Lyndsy Simon
  • 5,208
  • 1
  • 17
  • 21