views:

179

answers:

4

I have a PHP script that runs a .bat file on my windows machine using

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

This sets some environmental variables and is used to call Amazon EC2 API from the command line.

How do I do the same from a Linux server? I have renamed my .bat file to a shell (.sh) and changed the script to use 'export' when setting env vars. I have tested by running the code from a putty terminal and it does what it should. So I know the commands in the script are good. How do I run this from PHP? I have tried running the same command as above with the new filename and I don't get any errors, or file not found etc but it doesn't appear to work.

Where do I start trying to solve this?

---------------------------------- UPDATE -------------------------------

Here is the PHP script that calls the shell file -

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");    
}

And here is the .sh file - 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

I have been able to run this file from the command line so I know that the commands work ok. When I had this working on windows there would be a delay as the instance started up and I could echo the results to the screen. Now there is no delay as if nothing is happening.

A: 

have you tried shell_exec() ?

Osman Üngür
+2  A: 
$result = system("/bin/sh /path/to/shellfile.sh");
Mark
I don't know that you need execute permission if you're giving it directly to the shell ??
paxdiablo
ok i tried this but it still doesnt seem to run the file. Is the path to the file relative to the PHP script? I have checked permissions of the files and they are 777
undefined
Script doesn't need execute permissions because you're not executing it - you're executing `/bin/sh` and passing it the script to run.
meagar
@undefined Are you sure the contents of the file are valid commands in Linux? You said all you did was rename the file from .bat to .sh and add "export" in a few places. Unless your script is incredibly simple, this seems unlikely to produce a working shell script.
Tyler McHenry
yes thanks paxdiablo and meagar. I removed that incorrect info
Mark
@undefined, 777 is the number of the beast in UNIX land - not a good idea :-)
paxdiablo
undefined, yes it would be relative to the php script. If you are on a shared environment, check that safe_mode is not enabled
Mark
Yes its a very simple script - all it does is set up environmental variables for the EC2 API tools, sets the path to JAVA and then calls an EC2 API command. ... yes im aware that 777 is overkill but just wanted to make sure it wasnt a permissions problem.
undefined
+4  A: 

Put a hash-bang on the first line of your shell script.

#!/bin/bash

Then give it the executable flag.

$ chmod a+x yourshellscript

You can then call it from PHP with system.

$result = system("yourshellscript");
Didier Trosset
+1  A: 

Is script executable? If not, make it so:

$ chmod a+x script.sh          # shell

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

or launch it via interpreter:

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

Is interpreter specified in shell script (ie. #!/bin/sh line)?

el.pescado