Relative Path Not Working in Cron PHP Script

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

Cronjob relative path not executing the file

Add in your /etc/crontab file:

* *  * * *  /usr/bin/php /path/to/cronTest.php

To get the php path you can use which php command in your console.

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';

Enable proper relative path in cron

__FILE__ will specify the full file path of the current script.

realpath() translates paths with relative components into absolute paths.

This should work:

require_once (realpath(dirname(__FILE__)."/../file.php"));

crontab cannot read dir '../' in php script

.. references the parent directory of wherever the script is executing. It's presumably different in the two scenarios. Try cd /home/example.com/public_html/cron && /usr/bin/php file.php in your crontab, so that you're executing that in the same directory as your web page is.

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);
?>

PHP include_once works in local but call in cron not working

The $_SERVER variable is not set when running from CLI.
You have to use dirname(__FILE__) and make the paths relative to the current file.

For example in your case, something like:

include_once(dirname(__FILE__).'/../'.project_name.'/references/library.php');

Using Relative Paths in Crontab

After many trials and research, I discovered that the solution was using the HOME= variable, not the PATH= variable, like so:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=""
HOME=/var/www/html/crons

And then each of the lines would just look like:

*/2 * * * * root /usr/bin/php cronfile.php >> logs/cronfile_`date +\%Y\%m\%d`.log

Hope this helps someone else with the same issue I had in the future.

PHP Cron Job: Including file not working?

If you do require('../includes/common.php'), the path is traversed relative to the current working directory.

If you do require('common.php'), the file is searched in the include path, and in the directory of the script which calls the require().

To solve this, first change directory in your crontab:

cd /home/fini7463/public_html; php -f cronjob.php


Related Topics



Leave a reply



Submit