7

Synopsis

I'm getting this error:

Cron.php undefined index 'SCRIPT_FILENAME'

Why is the following code in the magento core cron.php?

// Only for urls
// Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

So my crontab has the following (in cPanel - yes, I hate cPanel but I have no choice):

*/5 * * * * /bin/sh /home/user/public_html/cron.sh 2>&1

When run, I get this output:

Array
(
    [MAILTO] => ***
    [SHELL] => /usr/local/cpanel/bin/jailshell
    [USER] => ***
    [PATH] => /usr/bin:/bin
    [PWD] => /home/***
    [LANG] => en_US.UTF-8
    [HOME] => /home/***
    [SHLVL] => 2
    [LOGNAME] => ***
    [_] => /usr/bin/php
    [PHP_SELF] =>
    [REQUEST_TIME_FLOAT] => 1427981401.51
    [REQUEST_TIME] => 1427981401


    [argv] => Array
        (
            [0] => /home/***/public_html/cron.php
        )

    [argc] => 1
)
3

As mam08ixo has stated certain _SERVER properties are available on the CLI, however, due to some abnormal configuration I solved this by using php-cli:

php-cli /home/***/public_html/cron.php > /dev/null &2>1
3

This is what worked for me:

php -f /home/USERNAME/www/cron.php >/dev/null 2>&1

And I had to modify cron.php, just use

$isShellDisabled = true;

instead of

$isShellDisabled = (stripos(PHP_OS, 'win') === false) ? $isShellDisabled : true;

Reference: http://support.xtento.com/wiki/Setting_up_the_Magento_cronjob

I kept getting Undefined index in my error log but it was working. However, to make it better, I initialized the variables in cron.php to remove the errors. Like so:

$_SERVER['SCRIPT_NAME'] = '';
$_SERVER['SCRIPT_FILENAME'] = '';
2

How come you "ultimately" think that the $_SERVER execution(!) environment information is not available when running scripts via CLI?

The PHP language reference contains some hints and comments that it actually does. Also, on any machine in my sphere, $_SERVER is populated just fine. So if you get an undefined index notice, it's most likely due to some specialties with your environment rather than a general fail in cron.php.

Pay special attention to the variables_order setting.

Appendix: You might want to regard $_SERVER as information acquired from the serving entity. May it be shell, mod_php, fastcgi, or whatever. That's why the contents (keys) of the variable depend on the current environment. Bottom line is, $_SERVER is NOT only populated when served via webserver.

2
  • I use: /usr/bin/php -d display_errors=1 -f cron.php and the errors occur. Also I have done a test with print_r($_SERVER) inside the cron.php and the indexes do not exist.
    – ash
    Apr 2 '15 at 11:36
  • based on your appendix, that is correct - but why does magento still error with undefined indexes - it's stupid.
    – ash
    Apr 2 '15 at 11:53
2

Since this is a top result in Google for missing SCRIPT_FILENAME with cron I will chime in.

For me, on a different ecom (CS-Cart on cPanel OpenVZ + FastCGI), cron's missing indexes were solved/found by using the path to php in cron instead of just php or php-cli. Run which php in shell or as then use that in cPanel cron builder. I'm theorizing that this may be due to cPanel using its own internal version of PHP, perhaps sprinkled with some FastCGI mysteries, although I'm still not exactly sure.

Examples:

php /home/account/public_html/script.php <- Fails

php-cli /home/account/public_html/script.php <- Fails

$(which php) /home/account/public_html/script.php <- Works in CLI as dynamic route, but will fail in cPanel cron builder

/usr/local/bin/php /home/account/public_html/script.php <- Works in cPanel cron builder, although it's a static route to php, so that may be different on your server

2
  • I remember initially having some problems with $(which php) when documenting my answer, however this should be useful to others, and generally is what I would use myself normally. I forgot if it was cPanel or Plesk that caused my problem though.
    – ash
    Jan 17 '17 at 15:33
  • @ash Yeah I hear ya about which php, especially with the CloudLinux/WHM multi php manager throwing env into other versions. Speaking of stuff others may need, I ran into more cron woes the other day trying to get Vtiger to validate/run correctly under php-cli. Made a little helper script that tries to expose more env infos (such as "_" index) if anyone needs for troubleshooting: gist.github.com/dhaupin/7c88be87a543e59f5be9fb55c7b963a9
    – dhaupin
    Jan 17 '17 at 15:49
1

There may be 2 solutions to this particular/system specific issue that I'm aware of:

In the crontab: Add the following 2 lines at the top of it:

SCRIPT_NAME="cron.php"

SCRIPT_FILENAME="cron.php"

OR

Wrap it into the CRON itself:

*/5 * * * * SCRIPT_NAME=cron.php; SCRIPT_FILENAME=cron.php; php ~/public_html/cron.php > /dev/null 2>&1

0

This is what worked for me on Godaddy Cpanel:

/usr/local/bin/php -q /home/USERNAME/public_html/path_to_cron.php >/dev/null 2>&1

And I had to modify cron.php, just use

$isShellDisabled = true;

instead of

$isShellDisabled = (stripos(PHP_OS, 'win') === false) ? $isShellDisabled : true;

Thank you Zurd for posting your answer which got me mine after soo much pain!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.