Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include path and cron

Tags:

php

I'm running a cronjob that calls a php script. I get "failed to open stream" when the file is invoked by cron. When I cd to the directory and run the file from that location, all is well. Basically, the include_once() file that I want to include is two directories up from where the php script resides.

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

like image 640
user109162 Avatar asked Dec 18 '22 07:12

user109162


2 Answers

There are multiple ways to do this: You could cd into the directory in your cron script:

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

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

include dirname(__FILE__) . '/../../' . 'includedfile.php';
like image 175
soulmerge Avatar answered Jan 04 '23 03:01

soulmerge


cron is notorious for starting with a minimal environment. Either:

  • have your script set up it's own environment;
  • have a special cron script which sets up the environment then calls your script; or
  • set up the environment within crontab itself.

An example of the last (which is what I tend to use if there's not too many things that need setting up) is:

0 5 * * * (export PATH = /mydir:$PATH ; myexecutable )
like image 35
paxdiablo Avatar answered Jan 04 '23 02:01

paxdiablo