What is the linux equivalent of running a .bat file on Windows with PHP using system ()

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

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

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

How can I do the same from a Linux server? I renamed my .bat file to shell (.sh) and changed the script to use 'export' when setting up env vars. I tested by executing the code from the 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 tried the same command above with the new filename and I don't get any errors, or the file was not found, etc., but it doesn't work.

Where do I start trying to solve this?

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

Here is a 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 was able to run this file from the command line so that I know the commands are working fine. When I was working on windows, there was a delay in starting the instance and I could echo the results on the screen. Now there are no delays, as if nothing is happening.

+2


a source to share


4 answers


Put the hashing on the first line of your shell script.

#!/bin/bash

      

Then give it an executable flag.



$ chmod a+x yourshellscript

      

Then you can call it from PHP using the system.

$result = system("yourshellscript");

      

+4


a source


$result = system("/bin/sh /path/to/shellfile.sh");

      



+2


a source


Does the script get executed? If not, do this:

$ chmod a+x script.sh          # shell

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

      

or run it through the interpreter:

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

      

Is the interpreter specified in the shell script (i.e. #!/bin/sh

)?

+1


a source


Have you tried shell_exec ()?

0


a source







All Articles