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 ;)
Answer from Brian on Stack OverflowI 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 ;)
You might also try giving the full path to the binary you're trying to run. That solved my problem when trying to use ImageMagick.
The question is a bit old, but for those who experience this problem can try to set the environment variables of direct on the server. PHP uses the putenv () function.
Example:// Set Variable Enviromental
$JAVA_HOME = "/usr/lib/jvm/java-8-oracle"; $ANDROID_HOME = "/opt/android-sdk-linux"; $PATH="$JAVA_HOME/bin:/usr/local/bin:/usr/bin:/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_HOME/build-tools/24.0.0-preview"; putenv ("JAVA_HOME = $JAVA_HOME"); putenv ("PATH = $PATH");
Most likely, your $PATH is set incorrectly. Specify the full path to android, or set your path via something like this:
<?php
putenv(
implode(PATH_SEPARATOR, array('/dir/containing/android', getenv('PATH')))
);
$fname = $_POST['fname'];
$fpack = $_POST['fpack'];
$email = $_POST['email'];
// Create a new Android project
var_dump(shell_exec("android create project --target 8 --name $fname --path ./$fname --activity MainActivity --package $fpack 2>&1"));
I would double check that you are running the command as the same user from the command line and your php script. That may be the problem. exec('whoami')
You said you had a script that starts your php script it should be the same user as that.
You might also want to running a simpler exec command to see if that will work first.
Other things to try:
- Try checking stderr output
exec('ls /tmp 2>&1', $out);This will redirect standard error to standard out so you get both. - Try using php's
shell_exec()which will invoke a shell just like when you are running from the command line(eg. bash).shell_exec('ls /tmp 2>&1 >> /tmp/log')should even put all output into a log file. - I don't think this will help you but it is something to try if all else fails, set it as a background process and see if it completes.
exec('nohup php process.php > process.out 2> process.err < /dev/null &'). The & will set the command to run in the background and let the script continue.
Good Luck
Is recins command accessible for PHP ? Also is path to books.rec correct ?
Try with absolute path.
The Apache’s user www-data need to be granted privileges to execute certain applications using sudo.
- Run the command
sudo visudo. Actually we want to edit the file inetc/sudoers.To do that, by usingsudo visudoin terminal ,it duplicate(temp)sudoersfile to edit. - At the end of the file, add the following ex:-if we want to use command for
restartsmokeping andphpcommand for another action in your question,
www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, usr/bin/php
(This is assuming that you wish to run restart and php commands using super user
(root) privileges.And you use php command in usr/bin/ path )
However, if you wish to run every application using
super user privileges, then add the following instead of what’s above.You might not want to do that, not for ALL commands, very dangerous.
www-data ALL=NOPASSWD: ALL
3.After edit the sudoers file(by visudo we edit the temp file of sudoers so save and quit temp file(visudo) to write in sudoers file.(wq!)
4.That’s it, now use exec() or shell_exec in the following manner inside your xxx.phpscript.keep remember to use sudo before the command use in the php script.
ex:-
exec ("sudo /etc/init.d/smokeping restart 2>&1");
or
shell_exec("sudo php -v");
So in your problem,add the commands that you wish to use in to the step no (2.) as I add and change your php script as what you want.
here is the same problem as yours https://stackoverflow.com/a/22953339/1862107
Try specifying the entire path to the php binary.. Eg, /usr/bin/php
If you don't know it, find it using: which php
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
shell_exec might not know what directory to look in for your executable's location directory. What solved it for me was this before the shell_exec:
putenv('PATH=/usr/local/bin');
Then the terminal can find the executable. Also check permissions on every part of the command to make sure apache user has read and execute permissions.