2012-08-24 140 views
2

在我專用的CentOS Linux服務器上,我的PHP CLI已停止工作。我有一個由開發人員編寫的需要CLI的腳本,這些腳本一直工作到昨天。同時發生的另一個奇怪之處在於/ usr/local/bin/to php中的符號鏈接丟失了。我重新創建了它,但它仍然無法工作。PHP CLI停止工作

我的開發者說它不工作,因爲當我們從命令行運行這個腳本時;

<?php 
echo 'php_sapi_name()='. php_sapi_name() .'<br />'; 
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) { 
echo "ENVIRONMENT IS CLI"; 
} ELSE { 
echo "ENVIRONMENT IS NOT CLI"; 
} 
//echo phpinfo(); 

在命令行中,則返回的是

php_sapi_name()=cgi-fcgi<br />ENVIRONMENT IS NOT CLI 

運行PHP -v給我下面的輸出

PHP 5.3.8 (cgi-fcgi) (built: Feb 2 2012 14:48:13) 
Copyright (c) 1997-2011 The PHP Group 
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies 
with Zend Guard Loader v3.3, Copyright (c) 1998-2010, by Zend Technologies 

最終改變我們的腳本來解決這個問題,但它仍然是奇怪,它只是如何改變

+4

有人更新/重新安裝你的php ;-)而且,不,我們不相信,沒有人能改變什麼:-) – zerkms

+0

就嘗試用命令的完整路徑運行腳本線php。即'/ usr/local/bin/php myscript.php'並共享結果。 – DhruvPathak

+0

我沒有運行完整路徑的腳本,並得到了以上結果 –

回答

0

試試這個:

<?php 
class CliCheck 
{ 
    /** 
    * Advanced PHP-CLI mode check. 
    * 
    * @return boolean Returns true if PHP is running from the CLI or else false. 
    * 
    * @access public 
    * @static 
    */ 
    public static function isCli() 
    { 
     // If STDIN was not defined and PHP is running as CGI module 
     // we can test for the environment variable TERM. This 
     // should be a right way how to test the circumstance under 
     // what mode PHP is running. 
     if(!defined('STDIN') && self::isCgi()) { 
      // STDIN was not defined, but if the environment variable TERM 
      // is set, it is save to say that PHP is running from CLI. 
      if(getenv('TERM')) { 
       return true; 
      } 
      // Now return false, because TERM was not set. 
      return false; 
     } 
     return defined('STDIN'); 
    } 

    /** 
    * Simple PHP-CGI mode check. 
    * 
    * (DSO = Dynamic Shared Object) 
    * 
    * @link http://httpd.apache.org/docs/current/dso.html DSO 
    * @link http://www.php.net/manual/en/function.php-sapi-name.php PHP_SAPI 
    * 
    * @return boolean Returns true if PHP is running as CGI module or else false. 
    * 
    * @access public 
    * @static 
    */ 
    public static function isCgi() 
    { 
     if (substr(PHP_SAPI, 0, 3) == 'cgi') { 
      return true; 
     } else { 
      return false; 
     } 
     return false; 
    } 
} 
print "PHP is running ".((CliCheck::isCgi()) ? "as CGI modul " : ((!CliCheck::isCli()) ? "as DSO modul " : "")); 
print ((CliCheck::isCli()) ? "from the CLI." : "."); 
print "\n"; 
?> 

來源:http://www.php.net/manual/fr/features.commandline.php#105043

+0

結果如下 –

+0

PHP運行來自CLI的CGI模塊 –