26

Here is the line from my cron job...

*/5 * * * * php /home/user/public_html/index.php --uri=minion --task=emailassets

When my script runs from this cron job, the PHP constant PHP_SAPI equals 'cgi-fcgi'. Why does PHP_SAPI not equal 'cli'?

mopsyd
  • 1,877
  • 3
  • 20
  • 30
Chad
  • 1,708
  • 1
  • 25
  • 44
  • 8
    Is it **always** cgi-fcgi or is it cli if you run it yourself from that command line on the server? If so, what happens when you run `which php` as yourself vs the cron-user? – Mike B Jun 04 '12 at 18:58
  • 14
    That was the problem. `which php` from the cron returned `/usr/bin/php` whereas from myself in the shell returned `/usr/local/bin/php`. So I specified the latter in my cron and all is well. – Chad Jun 04 '12 at 19:17
  • I am sorry for not beeing able to answer because this is closed as too localized but I found a nice function to check if the script is called from cli even if php is running in cgi-fcgi mode: http://www.binarytides.com/php-check-running-cli/ – steven May 29 '15 at 06:59
  • 3
    This is not too localized, and should not have been closed. Cli scripts (including cron) that invoke php will often return 'cgi' or 'cgi-fcgi', and occasionally other variants. This happens all the time. I suspect it was closed because someone looked at the tags and jumped to a conclusion rather than actually knowing anything about the matter. – mopsyd Nov 21 '17 at 07:59
  • 1
    This will solve your problem though: `if ( php_sapi_name() == 'cli' || strpos( php_sapi_name(), 'cgi' !== false) { /*command line*/} else { /*http*/ }`. That's all you need. – mopsyd Nov 21 '17 at 08:00

2 Answers2

7

From php.net manual:

The php_sapi_name() function is extremely useful when you want to determine the type of interface. There is, however, one more gotcha you need to be aware of while designing your application or deploying it to an unknown server.

Whenever something depends on the type of interface, make sure your check is conclusive. Especially when you want to distinguish the command line interface (CLI) from the common gateway interface (CGI).

Note, that the php-cgi binary can be called from the command line, from a shell script or as a Cron Job as well! If so, the php_sapi_name() will always return the same value (i.e. "cgi-fcgi") instead of "cli" which you could expect.

Do not always expect /usr/bin/php to be a link to php-cli binary.

Luckily the contents of the $_SERVER and the $_ENV superglobal arrays depends on whether the php-cgi binary is called from the command line interface (by a shell script, by the Cron Job, etc.) or by some HTTP server (i.e. Lighttpd).

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
gavintfn
  • 499
  • 6
  • 7
  • Currently "The PHP constant `PHP_SAPI` has the same value as `php_sapi_name()`." https://www.php.net/manual/en/function.php-sapi-name.php – Yousha Aleayoub Jul 29 '21 at 20:01
2

All credit goes to @mopsyd for this, but since the only answer does not provide a solution, I want to post this here so that it's easily readable instead of having to look through the comments.

tl;dr: You have cPanel installed which replaces PHP binaries with Perl scripts.

Use /usr/local/bin/php instead of /usr/bin/php.

You can test this like so:

$ diff /usr/bin/php /usr/local/bin/php
2c2
< # cpanel - php-cgi                                 Copyright 2018 cPanel, L.L.C.
---
> # cpanel - php-cli                                 Copyright 2018 cPanel, L.L.C.
7c7
< package ea_php_cli::cgi;
---
> package ea_php_cli::cli;
19c19
< my $bin = "php-cgi";
---
> my $bin = "php";

As you can see, /usr/bin/php is used for CGI requests and /usr/local/bin/php is used for CLI commands.

PatPeter
  • 394
  • 2
  • 17