Linux 从 cron 作业调用时,为什么 PHP_SAPI 不等于“cli”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10886539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 06:39:10  来源:igfitidea点击:

Why does PHP_SAPI not equal 'cli' when called from a cron job?

phplinuxcroncgi

提问by Chad

Here is the line from my cron job...

这是我的 cron 工作中的一行...

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

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

当我的脚本从此 cron 作业运行时,PHP 常量PHP_SAPI等于 'cgi-fcgi'。为什么PHP_SAPI不等于 'cli'?

回答by gavintfn

From php.net (https://www.php.net/manual/en/function.php-sapi-name.php)

来自 php.net ( https://www.php.net/manual/en/function.php-sapi-name.php)

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.

当您要确定接口类型时,php_sapi_name() 函数非常有用。但是,在设计应用程序或将其部署到未知服务器时,您还需要注意另一个问题。

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).

每当某些事情取决于接口类型时,请确保您的检查是决定性的。特别是当您想区分命令行界面 (CLI) 和通用网关界面 (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.

请注意,php-cgi 二进制文件可以从命令行、shell 脚本或作为 cron 作业调用!如果是这样,php_sapi_name() 将始终返回相同的值(即“cgi-fcgi”)而不是您所期望的“cli”。

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

不要总是期望 /usr/bin/php 是 php-cli 二进制文件的链接。

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, etc.) or by some HTTP server (i.e. lighttpd).

幸运的是,$_SERVER 和 $_ENV 超全局数组的内容取决于 php-cgi 二进制文件是从命令行界面(通过 shell 脚本、cron 等)调用还是通过某个 HTTP 服务器(即 lighttpd)调用.