【问题标题】:What is linux equivalent of running a .bat file on windows from PHP using system()linux 相当于使用 system() 从 PHP 在 Windows 上运行 .bat 文件
【发布时间】:2011-02-18 04:35:06
【问题描述】:

我有一个 PHP 脚本,它使用

在我的 Windows 机器上运行 .bat 文件

$result = system("cmd /C nameOfBatchFile.bat");

这会设置一些环境变量,用于从命令行调用 Amazon EC2 API。

如何在 Linux 服务器上执行相同的操作?我已将我的 .bat 文件重命名为 shell (.sh),并将脚本更改为在设置环境变量时使用“导出”。我已经通过从腻子终端运行代码进行了测试,它做了它应该做的事情。所以我知道脚本中的命令很好。如何从 PHP 运行它?我尝试使用新文件名运行与上述相同的命令,但没有收到任何错误或找不到文件等,但它似乎不起作用。

我从哪里开始尝试解决这个问题?

---------------------- 更新 ------------ -------------------

这是调用 shell 文件的 PHP 脚本 -

function startAmazonInstance() {
$IPaddress = "1.2.3.4"
    $resultBatTemp = system("/cmd /C ec2/ec2_commands.sh");
    $resultBat = (string)$resultBatTemp;
    $instanceId  = substr($resultBat, 9, 10);           
    $thefile = "ec2/allocate_address_template.txt"; 
    // Open the text file with the text to make the new shell file file
    $openedfileTemp = fopen($thefile, "r");
    contents = fread($openedfileTemp, filesize($thefile));
    $towrite = $contents . "ec2-associate-address -i " . $instanceId . " " . $IPaddress; 
    $thefileSave = "ec2/allocate_address.sh"; 
    $openedfile = fopen($thefileSave, "w");
    fwrite($openedfile, $towrite);
    fclose($openedfile);
    fclose($openedfileTemp);
    system("cmd /C ec2/mediaplug_allocate_address_bytemark.sh");    
}

这里是 .sh 文件 - ec2_commands.sh

#!/bin/bash
export EC2_PRIVATE_KEY=$HOME/.ec2/privateKey.pem
export EC2_CERT=$HOME/.ec2/Certificate.pem
export EC2_HOME=$HOME/.ec2/ec2-api-tools-1.3-51254
export PATH=$PATH:$EC2_HOME/bin
export JAVA_HOME=$HOME/libs/java/jre1.6.0_20
ec2-run-instances -K $HOME/.ec2/privateKey.pem -C $HOME/.ec2/Certificate.pem ami-###### -f $HOME/.ec2/aws.properties

我已经能够从命令行运行这个文件,所以我知道这些命令可以正常工作。当我在 Windows 上进行此操作时,实例启动时会有延迟,我可以将结果回显到屏幕上。现在没有任何延迟,好像什么都没有发生一样。

【问题讨论】:

    标签: php linux shell system


    【解决方案1】:
    $result = system("/bin/sh /path/to/shellfile.sh");
    

    【讨论】:

    • 不知道直接给shell需要执行权限吗??
    • 好的,我试过了,但它似乎仍然没有运行文件。文件的路径是相对于 PHP 脚本的吗?我检查了文件的权限,它们是 777
    • 脚本不需要执行权限,因为您没有执行它 - 您正在执行 /bin/sh 并将脚本传递给它以运行。
    • @undefined,777 是 UNIX 领域中野兽的数量 - 不是一个好主意 :-)
    • 未定义,是的,它与 php 脚本有关。如果您在共享环境中,请检查是否未启用安全模式
    【解决方案2】:

    脚本可以执行吗?如果没有,请这样做:

    $ chmod a+x script.sh          # shell
    
    system ("/path/to/script.sh"); // PHP
    

    或通过解释器启动它:

    system("sh /path/to/script.sh");        // PHP
    

    解释器是否在 shell 脚本中指定(即#!/bin/sh 行)?

    【讨论】:

      【解决方案3】:

      在你的 shell 脚本的第一行加上一个 hash-bang。

      #!/bin/bash
      

      然后给它可执行标志。

      $ chmod a+x yourshellscript
      

      然后您可以使用系统从 PHP 调用它。

      $result = system("yourshellscript");
      

      【讨论】:

        【解决方案4】:

        你试过 shell_exec() 吗?

        【讨论】:

          猜你喜欢
          • 2023-03-04
          • 2022-11-12
          • 2019-01-20
          • 2010-10-24
          • 2015-07-15
          • 1970-01-01
          • 2014-01-07
          • 1970-01-01
          相关资源
          最近更新 更多