Asked  7 Months ago    Answers:  5   Viewed   36 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


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?



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
Wednesday, March 31, 2021
answered 7 Months ago

You could use this class PHP-Parse-cron-strings-and-compute-schedules

It'll also compute the last scheduled run

Wednesday, March 31, 2021
answered 7 Months ago

Took me about 5 hours of Google and trying different things to FINALLY find the answer!

#ifdef __APPLE__
#include "CoreFoundation/CoreFoundation.h"

// ----------------------------------------------------------------------------
// This makes relative paths work in C++ in Xcode by changing directory to the Resources folder inside the .app bundle
#ifdef __APPLE__    
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef resourcesURL = CFBundleCopyResourcesDirectoryURL(mainBundle);
    char path[PATH_MAX];
    if (!CFURLGetFileSystemRepresentation(resourcesURL, TRUE, (UInt8 *)path, PATH_MAX))
        // error!

    std::cout << "Current Path: " << path << std::endl;
// ----------------------------------------------------------------------------

I've thrown some extra include guards because this makes it compile Apple only (I develop cross platform) and makes the code nicer.

I thank the other 2 guys for your answers, your help ultimately got me on the right track to find this answer so i've voted you both up. Thanks guys!!!!

Wednesday, June 23, 2021
answered 4 Months ago

When you say fetch('data.json') you are effectively requesting since it is relative to the page your are making the request from. You should lead with forward slash, which will indicate that the path is relative to the domain root: fetch('/js/data.json'). Or fully quality with your domain fetch('').

Thursday, July 1, 2021
answered 4 Months ago

Cronjob is not something to create as Php process or script. Cron is a linux program that allows you to call a script at a regular interval.

You can see what is an crontab by entering in your linux machine as an admin user and type:

root@valugi:~# crontab -e

You will see something like

*/1 * * * * /usr/bin/php /var/www/somesite/public/cron.php

This means that each minute I am executing the cron.php.

Now, you may want to have different scripts executed at different times and want to pass this logic to php level instead of linux level. If this is the case you may want to call your cron script at the lowest time denominator (minute for example) and in the cron.php build some logic that will call at different times other scripts.

I use for example a Cronable interface:

interface Cronable {
    public function cron();

And each class that wants to be called by the cron.php has to implement this interface and the cron() function, which will specify what is the specific frequency of the call. The cron.php will get all this classes and will compare current time with that time and will decide to execute the call or not.

Tuesday, July 20, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :