0

Is there any reason why I can not complied files in PHP's shell_exec/exec/system function?

Example of something that does work in command line and PHP's shell_exec function:

<?php
$data = shell_exec("ls");
echo $data;
?>

Example of something that does not work in PHP's shell_exec function but will work in command line (I can confirm that):

<?php
$data = shell_exec("./c-compiled-file argv1 argv2 argv3");
echo $data;
?>

Is there anything I can do on my server so this will work? I've looked everywhere and no solutions I found fixed the problem. The compiled file is in the same directory as the PHP script as well, it just won't execute it. Also if you're asking, yes I have tried this with SSH2 and it still will not execute.

Also PHP is not in safe mode and NO functions are disabled.

4
  • Did you try an absolute path? Did you enable error logging and check your logs files? Also try running the script via cli (php foo.php) instead of using a browser. Commented Sep 3, 2013 at 11:17
  • You should have +x mode on the file, chmod +x ./c-compiled-file or chmod 777 ./c-compiled-file Commented Sep 3, 2013 at 11:17
  • We are talking about same user and same working directory? Commented Sep 3, 2013 at 11:19
  • The compiled script is in the same folder as the PHP script doing the shell executing. Commented Sep 4, 2013 at 0:01

3 Answers 3

1

Some common glitches when executing external commands from PHP that work fine from shell:

  • Command uses relative paths but PHP is launched from an arbitrary location:

  • PHP and shell run with different user credentials. This is often the case when PHP runs through a web server.

  • PHP and shell run different commands. Many people call stuff like exec("foo $bar") and doesn't even check what "foo $bar" contains.

  • No error checking is done. The bare minimum is to capture and print standard output, standard error, status code and, of course, all PHP error messages including warnings and notices.

    • You can redirect stderr to sdtout
    • You can use a PHP function that allows to capture more information, such as exec()
       
  • The web server is disallowed to execute the command at operating system level.

    • Lookout for SELinux or similar tools.
Sign up to request clarification or add additional context in comments.

Comments

0

Just a guess, but the binary you're trying to execute might not have the proper permissions. Prepeding it with ./ in the command line forces it to execute, but PHP probably strips that for security purposes. Try this:

chmod +x c-compiled-file

1 Comment

Yes I have set proper permissions to the path as well.
0

You want to use system in the second case, and not shell_exec.

system executes an external program and displays the output.

shell_exec executes a command via shell and returns the complete output as a string.

and for good measure:

exec simply executes an external program.

Furthermore you want to make sure your external program is executable and (though you have stated it, I'll restate this) has execute permissions for the user which is running the web server. You also want to make sure the directory your external program is running in has the ability to write to its directory or /tmp or whatever output directory you have set.

Finally you should always use absolute paths for executing things like this in cron or php or whatever... so don't use ./c-compiled-file argv1 argv2 argv3, but instead use /home/username/c-compiled-file argv1 argv2 argv3 or whatever the full path is.

1 Comment

Yes, I have just tried this and echoed the output, it is the proper output but it seems to terminate right after starting, and this binary is not the problem, I've used this before on other servers but it seems this one it just screwed.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.