PHP is included in cron

I am trying to configure a PHP file as a cron job, where this PHP file contains other PHP files.

The file itself is located at /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/runner.php

The included file is located at /var/www/vhosts/domain.com/httpdocs/app/protected/config.php

How to include this configuration file from runner.php? I tried to do require_once ('../../config.php'), but he said that the file does not exist. I guess cron is running PHP from somewhere else or something.

The cron task is as follows.

/ usr / bin / php -q / var / www / vhosts / domain.com / httpdocs / app / protected / classes / cron / runner.php

Any thoughts?

+6
php cron
source share
4 answers

Your cron should change the working directory before running PHP:

cd /var/www/vhosts/domain.com/httpdocs/app/protected/classes/cron/ && /usr/bin/php -q runner.php 

Note that if the directory does not exist, PHP will not run runner.php.

+15
source share

You must use the absolute path. Cron probably does not run the script from the directory in which it is located.

I recommend using:

require_once( dirname(__FILE__) '../../config.php )

__FILE__ is a special constant that refers to the file you are in. dirname(...) will provide you with a directory that will evaluate the absolute path to the file you want to include.

+2
source share

Are classes or cron a symlink? I seem to remember that php evaluates the real path instead of the symbolic path.

Consider the following directory tree:

/target/index.php

/ path / sym → / target

if you must execute php index.php from /path/sym , then the require_once('../require.php'); statement require_once('../require.php'); will evaluate the value of require_once('/require.php'); not require_once('/path/require.php');

+1
source share

You can change the working directory inside your PHP script to the location of this script: chdir(__DIR__); (or, if your version of PHP is up to version 5.3: chdir(dirname(__FILE__)); ) Then you can do: require_once('../../config.php')

+1
source share

All Articles