Viewed   730 times

I have a certain PHP script that calls exec() to execute a command to convert a PDF to JPG. This command works fine in bash.

To preempt your initial troubleshooting guesses, note the following:

  • safe_mode = Off
  • Permission on the directory containing the PDF and the script is set to 777, and this directory is also where the JPG is being written.
  • The command I am passing to exec() explicitly points to the binary being used (e.g. /usr/local/bin/convert).
  • display_errors = On
  • error_reporting = E_ALL
  • disable_functions = [blank]
  • I am echoing exec()'s output and it returns nothing. The command being run by default returns nothing.

When I call this PHP script from the browser (visiting, exec() does not execute its argument.

IMPORTANT: I know that there are no issues with my script or the way I have constructed the bash command, because from bash, I can execute the script with 'php' and it works (e.g. 'php script.php' converts the file)

I have also tried switching out exec() with system().

Last, I have had this issue once before in the past but cannot remember how I fixed it.

I know there is something I am missing, so I hope someone else has experienced this as I have and remembers how to fix it!

Thank you in advance for any assistance you can provide.




Add 2>&1 to the end of your command to redirect errors from stderr to stdout. This should make it clear what's going wrong.

Monday, December 19, 2022

Try to redirect the standard error stream to stdout (by appending 2>&1 to the command), fetch that output and print it to check whether there was a meaningful error message

$cmd = "screen -dmS mcsrv java -Xmx512M -jar /var/www/srv/craftbukkit.jar -o true nogui";
$redirect = '2>&1';
// using variable substitution only for readability here 
shell_exec("$cmd $redirect", $output);
Thursday, August 25, 2022

exec — Execute an external program

system — Execute an external program and display the output

shell_exec — Execute command via shell and return the complete output as a string

so if you don't need the output, I would go with exec.

Further details:

Thursday, December 1, 2022

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

Thursday, September 1, 2022

Well, there's only two ways it can be disabled: safe_mode or disable_functions.

So you can do a check like:

function isAvailable($func) {
    if (ini_get('safe_mode')) return false;
    $disabled = ini_get('disable_functions');
    if ($disabled) {
        $disabled = explode(',', $disabled);
        $disabled = array_map('trim', $disabled);
        return !in_array($func, $disabled);
    return true;

Oh, and function_exists should return true, since it's a core function (otherwise you could forge a core function and cause some real havoc on a host)... Therefore is_callable should also return true (since the function does exist). So the only ways to tell, are to check the ini settings, or to actually call it...

Edit: One other thing to note, there are several of ways to execute shell commands. Check out:

  • Program Execution Functions
  • Backtick Operator
Sunday, September 18, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :