Topic on Project:Support desk

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

17
Summary last edited by GregRundlett 14:03, 26 April 2023 11 months ago

Some web hosts such as STRATO pass command-line PHP scripts to the cgi-fcgi interface rather than the CLI interface. Using php-cli in those environments works as intended.

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.

Zeros79 (talkcontribs)

Hi guys is there Any update information on if this bug will be fixed it's been a year and a bit since this ticket was opened and i'm also running into this problem and i will guess a lot of people who have strato as their host will get this problem as well with the only solution being doing a roll back to a older version.. would it maybe be a idea to take a page of the 1.30 version and not demand that a host has php-cli requirement .. and yes i have contacted strato and long story short unless you get a private server hosted by them instead of a shared virtual one you are basically up the creek as it were when it comes down to adding semantic-mediawiki to your mediawiki for example where you need to run the maintenance update script

2600:1700:275:1E50:4D93:CB02:1CDA:F93B (talkcontribs)

FYI I also just got this error. Like the Michael.seitz above my PHP_SAPI was set to 'cgi-fcgi'. I worked around it by changing maintenance/includes/Maintenance.php line 741 to exclude 'cgi-fcgi' in the if statement the displays the 'This script must be run from the command line' message.

       if ( PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg' && PHP_SAPI !== 'cgi-fcgi' ) {

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

       }

Ciencia Al Poder (talkcontribs)

Warning! your change may allow any maintenance script to be run from the web!

Kghbln (talkcontribs)

I just stumbled into this Strato madness. However, it is possible to force "php-cli" at Strato. See this help page for WordPress users. This also works for MediaWiki. I did

/opt/RZphp73/bin/php-cli maintenance/update.php --quick

and got the expected results. I hope this helps others with their Strato pain.

Ufnbg (talkcontribs)

yes, i can confirm - that works.

Michael.seitz (talkcontribs)

Thank you - that seems to work. Why couldn't strato tell me this solution when I asked them 4 years ago???