I use $_SERVER['DOCUMENT_ROOT']."/lib/sft_required.php"; to include the 'sft_required' file in a PHP script. When I run this file using browser, it works fine but when I run this as a cron job job, it does not work. It seems that the file is not included when we run the script through cron.
Once the PHP script is called for the first time by the crontab daemon, it can execute tasks in a time period that matches the logic of your application without keeping the user waiting. In this guide, you will create a sample cron_jobs database on an Ubuntu 20.04 server.
you could populate the $_SERVER['DOCUMENT_ROOT'] on your own
$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);
if the cron file is in document root
$_SERVER['DOCUMENT_ROOT'] = dirname(dirname(__FILE__));
if the cron file is one directory above the document root
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With