0

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)
jessie wind
  • 1
  • 1
  • 2

2 Answers2

1

Taken from: How to make a system call remotely?

The Apache’s user www-data need to be granted privileges to execute certain applications using sudo.

  1. Run the command sudo visudo. Actually we want to edit the file in etc/sudoers.To do that, by using sudo visudo in terminal ,it duplicate(temp) sudoers file to edit.
  2. At the end of the file, add the following ex:-if we want to use command for restart smokeping and mount command for another action,

www-data ALL=NOPASSWD: /etc/init.d/smokeping/restart, /bin/mount

(This is assuming that you wish to run restart and mount commands 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 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() 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");

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.

Community
  • 1
  • 1
Michael
  • 405
  • 4
  • 10
0

Try shell_exec and check return.

You can need sudo access too: sudo in php exec()

Buddy
  • 10,874
  • 5
  • 41
  • 58
brtsos
  • 373
  • 3
  • 14