<?php
$ret = exec('START C:\Program Files (x86)\Notepad++\Notepad++.exe', $output, $error);
// Debug
var_dump($ret);
var_dump($output);
var_dump($error);
?>
Update
maybe your php hasn't permissions to run commands on your wamp: https://stackoverflow.com/a/9161752/1721486
Answer from spinsch on Stack Overflow<?php
$ret = exec('START C:\Program Files (x86)\Notepad++\Notepad++.exe', $output, $error);
// Debug
var_dump($ret);
var_dump($output);
var_dump($error);
?>
Update
maybe your php hasn't permissions to run commands on your wamp: https://stackoverflow.com/a/9161752/1721486
on a mac, you'd need the command to actually open the app, like:
exec( 'open SomeApp.app' );
I think on Windows you would use 'start' (?)
exec( 'start notepad++.exe' );
PHP exec function passing a value to command line
Executing a shell command from PHP with shell_exec - Unix & Linux Stack Exchange
PHP exec() not working | php.ini is fine
Any way to SAFELY execute shell scripts from PHP?
Videos
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
On the php manual for shell_exec, it shows that the function returns the output as a string. If you expect output from the program you launch, you need to capture this like so:
$execQuery = "echo -n test_command";
$output = shell_exec($execQuery);
echo $output;
Your question doesn't show trying to capture any data. If you also make sure to connect stdout and stderr when you run your command, you should get a better idea is what is going on. To use your example:
$output = shell_exec("/usr/bin/oneuser create test10 test10 2>&1");
var_dump($output);
That should help you see what is going on. As Shadur suggests, it seems likely that these programs expect an interactive terminal that can enter passwords in order to run. Even if don't need input, they might expect interactive shells. And he's right that su doesn't play nice in this context. There is, however, a correct tool for the job.
You can setup sudo to such that your http user can execute your program as username without a password but NOT be able to do anything else by running visudo or whatever you use to edit your sudoers file and adding this line:
http ALL=(username) /usr/bin/oneadmin
Then in php your command would look something like this:
$execQuery = "sudo -u username /usr/bin/oneadmin postgres -c '/usr/bin/oneuser create test10 test10'";
$out = shell_exec ("$execQuery 2>&1");
echo $out
Try passthru($cmd);
It will allow user's I/O on the Terminal screen.
I have a website accessible from https://exampleurl.com:1234 (i.e. nonstandard port).
After logging in, this website gives my users a GUI and a few buttons. The buttons interact with PHP scripts on an Unraid VM, which then launch shell scripts. The shell scripts are used for controlling streams via streaming software called "OBS-Studio".
Here's an example:
my.php
<?php
$output = shell_exec("/bin/bash /var/www/html/streamstart.sh");
echo $output;
?>
streamstart.sh
#!/bin/bash
obs-cmd streaming startI'm relatively new to PHP, and I'm not sure how this could be exploited. Is there a way to make this more secure?
I have set the password for my users to be extremely secure, but that feels like the only line of defense right now.