我有一个PHP脚本,它在我的windows机器上运行.bat文件,使用
$result = system("cmd /C nameOfBatchFile.bat");
这将设置一些环境变量,并用于从命令行调用Amazon。
如何在Linux服务器上执行相同的操作?我已经将我的导出文件重命名为shell (.sh),并将脚本更改为在设置环境变量时使用‘.bat’。我已经通过从putty终端运行代码进行了测试,它做了它应该做的事情。所以我知道脚本中的命令很好用。我如何在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");    
}下面是ec2_commands.sh的.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上工作时,实例启动时会有延迟,我可以将结果回显到屏幕上。现在没有延迟,就好像什么都没有发生一样。
发布于 2010-05-11 22:45:01
在shell脚本的第一行加上一个hash-bang。
#!/bin/bash然后给它一个可执行的标志。
$ chmod a+x yourshellscript然后,您可以使用system从PHP调用它。
$result = system("yourshellscript");发布于 2010-05-11 22:44:00
$result = system("/bin/sh /path/to/shellfile.sh");发布于 2010-05-11 22:45:28
脚本是可执行的吗?如果不是,就这样做:
$ chmod a+x script.sh          # shell
system ("/path/to/script.sh"); // PHP或者通过解释器启动它:
system("sh /path/to/script.sh");        // PHP是在shell脚本中指定的解释器(即#!/bin/sh线路)?
https://stackoverflow.com/questions/2811627
复制相似问题