Writing this down after spending a few hours figuring what went wrong…was adding a cron job to the root crontab for a PHP script which ran fine by itself but not so when executed by the cron job.
Entry in root crontab (sudo crontab -e
):
# Run PHP script every 3 hours between 9am - 6pm SGT. Time below is in UTC 0 1-10/3 * * * /usr/bin/php /var/www/scripts/test.php >/dev/null 2>&1
Directory contents where PHP script resides:
/var/www/scripts - test.php - config.php - log.txt
Contents of original PHP script:
<php // Does not work when executed by cron job $configFile = 'config.php'; $config = include $configFile; // file returns array // Does not work when executed by cron job $logFile = 'log.txt'; file_put_contents($logFile, json_encode($config));
Contents of fixed PHP script:
<php // Works when executed by cron job $configFile = __DIR__ . '/config.php'; $config = include $configFile; // file returns array // Works when executed by cron job $logFile = __DIR__ . '/log.txt'; file_put_contents($logFile, json_encode($config));