24

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

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

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

4

2 回答 2

6

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

于 2020-01-16T06:45:36.037 回答
0

所有的功劳都归功于@mopsyd,但由于唯一的答案没有提供解决方案,我想在这里发布这个,以便它易于阅读,而不必查看评论。

tl;dr:您安装了 cPanel,它用 Perl 脚本替换 PHP 二进制文件。

使用 /usr/local/bin/php 而不是 /usr/bin/php。

你可以这样测试:

$ 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";

如您所见,/usr/bin/php 用于 CGI 请求,/usr/local/bin/php 用于 CLI 命令。

于 2021-06-26T18:14:01.953 回答