【发布时间】: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 上进行此操作时,实例启动时会有延迟,我可以将结果回显到屏幕上。现在没有任何延迟,好像什么都没有发生一样。
【问题讨论】: