1

I'm trying to run cron php script from command line. The server does not have PHP CLI but php-cgi exists. I tried to pass PHP directly to php-cgi but this does not work because two server environment variables are missed ($_SERVER['SCRIPT_FILENAME'] and $_SERVER['SCRIPT_PATH']). I tried to fill variables using following recommendation but these variables are not populated. What is missed in this answer? How to make php-cgi populate these server values? The question is not platform specific, but execution script looks like this currently

#!/bin/bash

export REDIRECT_STATUS=200
export GATEWAY_INTERFACE="CGI/1.1"
export SCRIPT_FILENAME=/usr/local/www/owncloud/cron.php
export REQUEST_METHOD="GET"

/usr/local/bin/php-cgi 

Please note that I'm not asking for workaround, I already added helper php file which fill necessary values. But this is temporary solution because next software update could show another issue with php-cgi call

UPDATE

It seems the issue is not with php-cgi execution but with the SCRIPT_FILENAME and SCRIPT_PATH server variables only. So solution works in common cases when you do not need them. Also the issue might be version specific (it works with PHP version prior to 5.5) or platform specific

Community
  • 1
  • 1
Andrey
  • 722
  • 2
  • 8
  • 17
  • do you require the additional `export GATEWAY_INTERFACE` line? – bansi Sep 23 '13 at 15:29
  • have you tried `setenv` instead of `export`? – bansi Sep 23 '13 at 15:34
  • no, it was mistake in the code. But it does not affect the php-cgi behavior. export is bash equivalent of setenv. And setenv command does not exist for bash – Andrey Sep 23 '13 at 15:40
  • what you are doing is correct, tested and it works for me. have you checked if you have got sufficient permission? – bansi Sep 23 '13 at 15:53
  • Do you check your php can access mentioned variable? Is it filled with correct path? – Andrey Sep 23 '13 at 16:04

2 Answers2

2

I searched SO with question to these specific server variables and found following statement here:

You are just juggling variables now. SCRIPT_FILENAME is a part of the CGI spec. It will not be available if PATH_INFO is unavailable. As for REQUEST_URI, it's apache's mod_rewrite specific. – LiraNuna

So exporting PATH_INFO variable also populates values from SCRIPT_FILENAME and SCRIPT_PATH environment variables. Please note that SCRIPT_FILENAME is needed still to point php-cgi to php input file. Below is the final script

#!/bin/bash

export REDIRECT_STATUS=200
export GATEWAY_INTERFACE="CGI/1.1"
export REQUEST_METHOD="GET"
export SCRIPT_FILENAME=/usr/local/www/owncloud/cron.php
export SCRIPT_PATH=cron.php
export PATH_INFO=$SCRIPT_FILENAME

/usr/local/bin/php-cgi 
Community
  • 1
  • 1
Andrey
  • 722
  • 2
  • 8
  • 17
1

Here is what i tested. i used root account on my home PC. My IP is masked from the output. cannot post this much as comment so posting as answer.

The script

#!/bin/bash

export REDIRECT_STATUS=200
export GATEWAY_INTERFACE="CGI/1.1"
export SCRIPT_FILENAME=/root/t.php
export REQUEST_METHOD="GET"

/usr/bin/php-cgi

PHP file

<?php
print_r($_SERVER);
?>

Output I get

X-Powered-By: PHP/5.3.8
Content-type: text/html

Array
(
    [HOSTNAME] => HomePC
    [SELINUX_ROLE_REQUESTED] =>
    [SHELL] => /bin/bash
    [TERM] => xterm
    [HISTSIZE] => 1000
    [SSH_CLIENT] => xx.xx.xx.xx 58196 22
    [SELINUX_USE_CURRENT_RANGE] =>
    [SSH_TTY] => /dev/pts/3
    [USER] => root
    [LS_COLORS] => rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
    [SCRIPT_FILENAME] => /root/t.php
    [PATH] => /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
    [MAIL] => /var/spool/mail/root
    [PWD] => /root
    [LANG] => en_US.UTF-8
    [REDIRECT_STATUS] => 200
    [SELINUX_LEVEL_REQUESTED] =>
    [HISTCONTROL] => ignoredups
    [HOME] => /root
    [SHLVL] => 2
    [LOGNAME] => root
    [SSH_CONNECTION] => xxx.xxx.xx.xxx 58196 192.168.1.2 22
    [GATEWAY_INTERFACE] => CGI/1.1
    [LESSOPEN] => ||/usr/bin/lesspipe.sh %s
    [REQUEST_METHOD] => GET
    [G_BROKEN_FILENAMES] => 1
    [_] => /usr/bin/php-cgi
    [PHP_SELF] =>
    [REQUEST_TIME] => 1379952948
)
bansi
  • 55,591
  • 6
  • 41
  • 52
  • Unfortunately if does not work for me. Also I do not see SCRIPT_NAME server variable in your output. It is also required by cron script – Andrey Sep 23 '13 at 16:37
  • I think if you execute through CLI you get only `SCRIPT_FILENAME` http://www.php.net/manual/en/reserved.variables.server.php isn't `SCRIPT_FILENAME` good enough for your case? – bansi Sep 23 '13 at 17:05
  • cron.php fails with missed `SCRIPT_NAME` variable as well. Anyway I do not see `SCRIPT_FILENAME` in server variables as well. It could be PHP version specific, but I use newer version of PHP (`X-Powered-By: PHP/5.5.2`) – Andrey Sep 23 '13 at 17:08
  • if you really want `SCRIPT_NAME` you can add `export SCRIPT_NAME=/root/t.php`. This also fills the `PHP_SELF` variable. – bansi Sep 23 '13 at 17:15
  • May be version specific. I cannot have access to PHP 5.5.x now. can check tomorrow morning my time. – bansi Sep 23 '13 at 17:22
  • I tested this in `fc18 PHP 5.4.19` and works for me. 5.4.19 is the latest version available for my distro (don't want to compile from source). Sry I may not be able to help further. – bansi Sep 24 '13 at 03:35
  • Thank you anyway. It seems I found the reason of such behavior – Andrey Sep 24 '13 at 06:57
  • Please post what you have found as an answer and accept it. It may help someone – bansi Sep 24 '13 at 06:59