Include path and cron

I run a cronjob that calls a php script. I get "could not open stream" when the file is called cron. When I connected to the directory and ran the file from this place, everything is fine. Basically, the include_once () file that I want to include consists of two directories from which the PHP script is located.

Can someone please tell me how can I get this to work from a cronjob?

+4
source share
3 answers

There are several ways to do this: you could cd into a directory in a cron script:

 cd /path/to/your/dir && php file.php 

Or specify the correct include file relative to the current script in PHP:

 include dirname(__FILE__) . '/../../' . 'includedfile.php'; 
+5
source

cron is known as starting with a minimal environment. Or:

  • configure the script your own environment;
  • has a special cron script that installs the environment and then calls your script; or
  • set up the environment inside crontab itself.

An example of the latter (which I usually use if there are not many things that need to be configured):

 0 5 * * * (export PATH = /mydir:$PATH ; myexecutable ) 
+5
source

you need to find out what the path cron is running from.

  echo pathinfo($_SERVER["PATH_TRANSLATED"]); 

accordingly enable

 include $path_parts['dirname']."/myfile.php"; 
+1
source

All Articles