Viewed   205 times

If a PHP script is run as a cron script, the includes often fail if relative paths are used. For example, if you have

require_once('foo.php');

the file foo.php will be found when run on the command line, but not when run from a cron script.

A typical workaround for this is to first chdir to the working directory, or use absolute paths. I would like to know, however, what is different between cron and shell that causes this behavior. Why does it fail when using relative paths in a cron script?

 Answers

3

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
Thursday, August 4, 2022
5

There are no problems running a PHP script as a cron job. What you need to do is provide the full path in the filesystem to the script file, and use the command php to tell Linux what program to run the file with.

Example:

*/5 * * * * php /var/www/vhosts/statistikk/cron/getLastArticleFromIntranet.cron.php >> /var/www/vhosts/statistikk/cron/LOG.txt 2> /dev/null

This script will run every 5 minutes, all days of the week. Whatever the PHP-script echoes / prints out, will be stored to the LOG.txt file so that I can monitor the scripts events.

Try running just the command in your shell before putting it in the cronjobs to make sure it works.

However, you say that you normally call this script with a AJAX call. This will not be possible to do with a cronjob. I assume you use AJAX to pass along some $_POST-elements that the script needs. So what you have to do is either adapt the script to allow $argv parameters as well, and add them to the crontab job, or simply make a script which does not need any given parameters before it runs.

If you are going to adapt your script to support $argv parameters, follow the answer already existing on about adding parameters to the job:

How to run a php url with parameters in cron tab

EDIT:

I'd just like to add to my answer as from the answer below. To edit you crontab jobs in Linux you can simply use the command crontab -e.

This is the description of each required param that needs to be filled.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)
Wednesday, September 21, 2022
 
lhencq
 
5

Looks like that you copied your full php.ini from your windows machine (probably the dev environment) up to a remote server which is running some linux distribution.

The extensions listed in your xampp provided php.ini won't work this way over there. If you are certain that you want to copy the whole php.ini from your dev machine, (you most likely won't need that) you will probably have to remove or edit the extension= lines to fit the environment (They most likely should end with .so at least and have a different path). Checking every path related setting would be wise too.

(Using full paths in every extension= line is foolish since there's extension_dir)

Wednesday, November 9, 2022
 
xxx
 
xxx
4

Assuming you are running the script directly through cron (as opposed to from a web server accessed by an HTTP request triggered by a cronjob (e.g. by cron running wget)), then of course it doesn't work.

There is no server, so $_SERVER is not set.

Thursday, November 10, 2022
5

If you don't want to mess up with the src attributes throughout your website, you may consider changing configuration directives.

You can relocate it by editing the DocumentRoot setting in C:xamppapacheconfhttpd.conf.

It should be currently set as :

C:/xampp/htdocs

Change it to:

C:/xampp/htdocs/exampleSite

and your relative link as <img src="/images/sidenav/analysis-2.gif" /> should be working fine.

NOTE:

  • Don't forget to restart your XAMPP Server after making changes.
  • After these changes, your leading / will always direct to the exampleSite folder. Should you decide to change the root later, repeat the process for the root folder of your choice.
Thursday, October 6, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :