Topic on Project:Support desk

Jump to navigation Jump to search

Starting with 1.31. I get "This script must be run from the command line" when I run a php-scripts via ssh

11
Summary by MarkAHershberger
Michael.seitz (talkcontribs)

Hallo,

This is my setting:

Software     Version

MediaWiki     1.31.1

PHP     7.2.10 (cgi-fcgi)

MySQL     5.6.42-log

ICU     49.1.2

My webhoster is Strato. I use putty for the ssh access.

Up to Mediawiki 1.30 it is no problem to run php-scripts like runJobs.php from the command line with ssh/putty. Starting with Mediawiki 1.31 I get the error message "This script must be run from the command line".

I compared Maintenance.php from 1.30 and 1.31 and noticed that the code has changed:

1.30 line 642 - 645:

       # Abort if called from a web server

       if ( isset( $_SERVER ) && isset( $_SERVER['REQUEST_METHOD'] ) ) {

           $this->error( 'This script must be run from the command line', true );

       }

       

1.31 line line 690 - 694:

       # Abort if called from a web server

       # wfIsCLI() is not available yet

       if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' ) {

           $this->fatalError( 'This script must be run from the command line' );

       }

       

I disabled this part of the code in the 1.31 version of Maintenance.php and everything works fine. The php-scripts run as expected.

This is a temporary solution, but how can it be solved without disabling this part of the code?

MarkAHershberger (talkcontribs)
Michael.seitz (talkcontribs)

Thank's a lot.!

As far as I understand your reply developers will work on this issue.

If there is a patch I can test, I am quite willing to do it.

Legoktm (talkcontribs)

Can you run eval.php and tell us what the value of PHP_SAPI is (echo PHP_SAPI;)?

That would help a lot in figuring out what's wrong.

Michael.seitz (talkcontribs)

I used my Maintenance.php without the code on lines 690 - 694 (otherwise I would get "This script must be run from the command line").

In the maintenance directory I typed "php eval.php" on the command line -> the cursor moved to a new line. No prompt etc. was visible.

I typed "echo PHP_SAPI;" and I didn't see anything on the screen, but when I pressed enter, I got the following result:

X-Powered-By: PHP/7.2.10

Content-type: text/html

cgi-fcgi

I had to press CTRL-C to abort the script.

Legoktm (talkcontribs)

Thanks. So for some reason it seems that running php is actually starting up the fcgi version of PHP, which is pretty wrong. Does php-cli whateverscript.php work? I found https://github.com/owncloud/core/issues/15054 which is on the same hosting provider as yours. I would also suggest filing a support ticket with your hoster asking how they want you to run command-line PHP scripts.

Michael.seitz (talkcontribs)

Sorry, I was quite busy and I couldn't answer earlier.

I tried php-cli showJobs.php and php-cli runJobs.php and got the message "php-cli: Command not found." Does this give any further information concerning what I should write to my provider?

MarkAHershberger (talkcontribs)

It looks like there is no php-cli command. Have you contacted your hosting provider yet?

Michael.seitz (talkcontribs)

Not yet. I am rather busy right now. I hope I get a chance in the middle of next week..

Michael.seitz (talkcontribs)

Finally I got a chance to write Strato support. I described the problem with the maintenance.php script and told them that the MediaWiki support desk wrote that "running php is actually starting up the fcgi version of PHP," and asked me to ask you "how they want you to run command-line PHP scripts."

I got the answer that basically says they offer the installation of MediaWiki using their AppWizard and that there should be no problems.

One recommendation they gave was "should the support of MediaWiki refer to fast CGI, I recommend to disable PHP boost in the setting of the php version." (translated from German).

I went to the setting and found PHP boost. They describe it as follows: "PHP boost activates a code cache, which will make your PHP-scripts run up to 100% faster" (translated from German).

I deactivated it and tried to run showJobs.php and the same message appeared "This script must be run from the command line".

I ran the eval.php the way you asked me to do and got the same result: cgi-fcgi.

Obviously php-boost is - as I suspected - only about caching and has nothing to do with the way how php scripts are run.

Summary: the answer from Strato was not helpful.

Michael.seitz (talkcontribs)

I did some research and found e.g. the following page: https://www.binarytides.com/php-check-running-cli/.

As far as I understand it says checking if php is running from the command line using the PHP_SAPI constant may produce wrong results: "It will work only if the cli mode has been triggered by the php cli binary. If php-cgi was used to initiate the script from the command line, it would always return "cgi-fcgi" for example." This is exactly what happens in my case.

Based on their "Working Solution" I tried the following code in maintenance.php (starting on line 690):

       # Abort if called from a web server

       # wfIsCLI() is not available yet

       function is_cli()

       {   if( defined('STDIN') )

           {

               return true;

           }

           if( empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0)

           {

               return true;

           }

           return false;

       }

       if ( is_cli() == false ) {

           $this->fatalError( 'This script must be run from the command line' );

       }

It seems to work. When I call e.g. shoJobs.php from the command line it does, what it should do. I also tried to call it from a browser I get "This script must be run from the command line". Of course I had to disable .htaccess first otherwise I only get "You don't have permission to access /maintenance/showJobs.php on this server."

I only have some very basic knowledge in writing php-code and it may not yet be the perfect solution.

Reply to "Starting with 1.31. I get "This script must be run from the command line" when I run a php-scripts via ssh"