2

Hi I'm trying executing a bash script through PHP, when the script runs the shell interface appears for a few seconds and then disappears.

However what I need is a way to run the bash script without the shell appearing at all. Found the execInBackground function on the php website shown below but it isn't working for me. What would be the best way to do this?

function convert($tmpName, $fileName, $fileSize, $fileType){
    $old_path = getcwd();
    chdir('C:\xampp\htdocs\p-master\tools');
    execInBackground("test.sh");
    chdir($old_path);
}

function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 

Edit: The shell window that appears is Git Bash

Edit 2: Still having this problem, tried all the suggested solutions and tried different ones from around the web but no luck, can anyone help with this?

5
  • I'm guessing it's a Cygwin window that's appearing. Windows wouldn't natively have the faintest idea what to do with a shell script... would it? (based on chdir('C:\xampp\htdocs\p-master\tools'); I'm assuming you're running Windows) You're telling cmd to start \B test.sh
    – CD001
    Feb 4, 2016 at 14:20
  • The window is Git Bash and yep I am running Windows.
    – olliejjc16
    Feb 4, 2016 at 14:27
  • I'm not 100% on this but I'm not sure the background flag (/b) will actually work here - that will ensure the cmd prompt window doesn't appear not necessarily the following Bash window. You could try replacing start with the path to sh.exe and using the & flag as you'd do with *nix ... maybe.
    – CD001
    Feb 4, 2016 at 14:34
  • hi thanks for that tried it but no luck I'm afraid!
    – olliejjc16
    Feb 4, 2016 at 14:43
  • This refers to Cygwin ... but it might offer some help; I'm a little out of my depth with Bash on Windows I'm afraid ;) stackoverflow.com/questions/673278/…
    – CD001
    Feb 4, 2016 at 14:51

1 Answer 1

0

You can use the shell_exec function which is built into PHP. It will execute a command via the shell and return the complete output as a string or a null value if an error occurred.

You also need to take the script you are executing out of the test.sh file and put it into the PHP function passing it to the shell_exec function as the parameter. The reason why the window keeps opening is that what you are doing here is effectively the same as double clicking on a file on the computer to open it, you are effectively telling the command line to open this file in a command line window and then run the script which is in it.

Syntax

string shell_exec(string $cmd);

Sources

http://php.net/manual/en/function.shell-exec.php

4
  • Hi tried that initially but the window still appears, the script does work but I need it to work without the shell window appearing!
    – olliejjc16
    Feb 4, 2016 at 14:44
  • Please see the edit I have made to the answer regarding shifting the code out of the test.sh file and placing it into the PHP file, passing it as a parameter to the shell_exec function. I have tested this on my machine and it worked fine. Feb 4, 2016 at 14:53
  • 1
    you are effectively telling the command line to open this file in a command line window and then run the script which is in it ... except he's using the /b flag which should tell cmd not to open a window - it's the Git Bash window that's appearing... but yeah, moving the shell code into the PHP and using shell_exec() should neatly sidestep that issue.
    – CD001
    Feb 4, 2016 at 14:53
  • Hi how do you write the command in directly? I have it like this but it's not working shell_exec("pyang -f yin -o H:\\YangModels\\yin\\types-ipos.yin types-ipos.yang");
    – olliejjc16
    Feb 4, 2016 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.