I am trying to create a tmux session and run a few other commands in it via php.
When i run the script from web it will not execute the commands, but when i run the script from terminal with "php test.php" it works.
I have tried to run the cmds as sudo and turn off the sudo pass for www-data. But still the commands won't execute.
Script:
$array_output = array();
$cmd = "sudo tmux new-session -s Server-17 -d ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
$cmd = "sudo tmux send -t Server-17 'cd /home/Minecraft/Servers/1/17/' ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
$cmd = "sudo tmux send -t Server-17 'chmod +x start_script.sh' ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
$cmd = "sudo tmux send -t Server-17 'sh start_script.sh' ENTER 2>&1";
$output = shell_exec($cmd);
array_push($array_output, $output);
var_dump($array_output);
Output:
array (size=4)
0 => null
1 => string 'failed to connect to server
' (length=28)
2 => string 'failed to connect to server
' (length=28)
3 => string 'failed to connect to server
' (length=28)
Taken from: How to make a system call remotely?
The Apache’s user
www-dataneed to be granted privileges to execute certain applications usingsudo.sudo visudo. Actually we want to edit the file inetc/sudoers.To do that, by usingsudo visudoin terminal ,it duplicate(temp)sudoersfile to edit.www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, /bin/mount(This is assuming that you wish to run
restartandmountcommands using super user (root) privileges.)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
ALLcommands, very dangerous.3.After edit the sudoers file(by
visudowe edit the temp file ofsudoersso save and quit temp file(visudo) to write insudoersfile.(wq!)4.That’s it, now use
exec()in the following manner inside yourxxx.phpscript.keep remember to usesudobefore the command use in the php script.ex:-
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.