Asked  8 Months ago    Answers:  5   Viewed   44 times

I am using this php code:

exec("unrar e file.rar",$ret,$code);

and getting an error code of illegal command ie 127 ... but when I am using this command through ssh its working ... because unrar is installed on the server ... so can anyone guess why exec is not doing the right stuff?

 Answers

95

Try using the direct path of the application (/usr/bin/unrar of whatever), it sounds like php can't find the application.

Wednesday, March 31, 2021
 
PLPeeters
answered 8 Months ago
60

exec() will block until the process you're exec'ing has completed - in otherwords, you're basically running your 'test.php' as a subroutine. At bare minimum you need to add a & to the command line arguments, which would put that exec()'d process into the background:

exec("php test.php {$test['id']} &");
Saturday, June 19, 2021
 
motanelu
answered 5 Months ago
75

I already said that I was new to exec() function. After doing some more digging, I came upon 2>&1 which needs to be added at the end of command in exec().

Thanks @mattosmat for pointing it out in the comments too. I did not try this at once because you said it is a Linux command, I am on Windows.

So, what I have discovered, the command is actually executing in the back-end. That is why I could not see it actually running, which I was expecting to happen.

For all of you, who had similar problem, my advise is to use that command. It will point out all the errors and also tell you info/details about execution.

exec('some_command 2>&1', $output);
print_r($output);  // to see the response to your command

Thanks for all the help guys, I appreciate it ;)

Monday, July 12, 2021
 
dkcwd
answered 4 Months ago
30
function php_exec($file_path) {
    if (!($binary = which(array('php', 'php5', 'php-cli', 'php-cgi'))))
        return false;

    return exec("$binary $file_path > /dev/null 2>/dev/null &");
}

function which($binaries) {
    if (!($path = getenv('PATH')) && !($path = getenv('Path')))
        return false;

    $arr = preg_split('/[:;]/', $path);

    foreach ($arr as $p) {
        foreach ($binaries as $b) {
            if (file_exists("$p/$b"))
                return "$p/$b";
        }
    }   

    return false;
}

var_dump(php_exec('test.php'));

Explanation: On most systems the PHP binary is called php, php5, php-cli or php-cgi. which() function checks the standard path (both *nix and windows have environment variable called PATH/Path) for each of those names and if it find a file with such name, it will return it.

The PATH variable's format is: /bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin for *nix (bash), and C:WindowsSystem32;C:Windows; for windows, so that's why I use preg_split('/[:;]/')

This solution is better than yours, because php_exec() will return false if it can't find a valid php binary. In your solution there's no way to know if the script execution failed.

Thursday, August 26, 2021
 
Lloydworth
answered 2 Months ago
29

You can implement your own test runner, for example by extending PHPUnit_TextUI_TestRunner and make it collect and print run times.

Friday, September 24, 2021
 
Stuart Blackler
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :