Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$_SERVER['DOCUMENT_ROOT'] does not work in the php script running through cron

Tags:

php

cron

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.

like image 294
phpian Avatar asked Jan 20 '10 10:01

phpian


People also ask

Can Cron run PHP script?

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.


2 Answers

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

like image 58
Gabriel Solomon Avatar answered Sep 22 '22 17:09

Gabriel Solomon


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.

like image 22
Quentin Avatar answered Sep 21 '22 17:09

Quentin