Php: Require Path Does Not Work for Cron Job

PHP: Require path does not work for cron job?

Technically seen the php script is run where cron is located; ex. If cron was in /bin/cron, then this statement would look for common.php in /bin/includes/common.php.

So yeah, you'll probably have to use fullpaths or use set_include_path

set_include_path('/home/username123/public_html/includes/');
require 'common.php';

PHP cron require path issue

There is no $_SERVER when running php-cli. It will return empty or null value.

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.

https://secure.php.net/manual/en/reserved.variables.server.php

Replace with a path to the file you want, assuming the path of the script you are running. This may work, but maybe you need to add or remove some of the ../

require(__DIR__ . '/../../../files/core/config.php');

The error you get points to config.php file. You probably have another $_SERVER there, that you need to replace and/or find a way to identify whether is a HTTP request or php-cli.

Something like

if($_SERVER['DOCUMENT_ROOT']) {
require($_SERVER['DOCUMENT_ROOT'] . ...);
} else {
require(__DIR__. ...);
}

You may want to add a global constant that points to the root of you project.

if($_SERVER['DOCUMENT_ROOT']) {
const BASEDIR = $_SERVER['DOCUMENT_ROOT'];
} else {
const BASEDIR = __DIR__. ...;
}

Then use BASEDIR for the entire application.

EDIT: as suggested by @YvesLeBorg, you can create a different file, that call your entry point with curl or wget.

*/5 * * * * /usr/bin/php /path/to/my_script.sh

Then in my_script.sh you can write

wget http://my_web_page/shop.php

Then you will have a $_SERVER and there is no need to refactor.
Be aware of security, as anyone can call you page and run your proccess.
You may want to use a token and validate IP Address to be sure that only you can call this shop.php page.

Relative path not working in cron PHP script

The working directory of the script may be different when run from a cron. Additionaly, there was some confusion about PHPs require() and include(), which caused confusion about the working directory really being the problem:

include('foo.php') // searches for foo.php in the same directory as the current script
include('./foo.php') // searches for foo.php in the current working directory
include('foo/bar.php') // searches for foo/bar.php, relative to the directory of the current script
include('../bar.php') // searches for bar.php, in the parent directory of the current working directory

PHP Include Not Working in Cron Job

I had several problems running cron scripts from Cpanel with absolute paths (when having includes) and ended up changing to executing them as URL's, now everything is working fine:

My command

wget -O /dev/null http://www.example.com/somescript > /dev/null 2>&1

Cron job not working when cron file is located outside public_html directory

An alternative is to use the lynx command (but you must have lynx installed, say yum install lynx )

1 * * * * /usr/bin/lynx http://mywebsite.com/cron1.php -dump

If you want to use PHP instead , you may try to change

<?php
$path = dirname(__DIR__) . "/data/cronresults.txt";
file_put_contents($path, "random text\n", FILE_APPEND);
?>

to absolute path (such as the following, but adjust it according to your real path)

<?php
$path = "/var/www/websitename/data/cronresults.txt";
file_put_contents($path, "random text\n", FILE_APPEND);
?>

Crontab and PHP require_once not working well

The cronjob is run from the home directory of the user that the crontab is associated with. There are a couple of ways to get relative paths working correctly from a cronjob like this, but the easiest in my opinion is to just change directories before calling the script.

40 14 * * * root cd /home/sites/sitename/batch && php sendemailtest.php


Related Topics



Leave a reply



Submit