PHP tmux and screen dont behave correctly (no launch, no new session, no new windows)

1.1k views Asked by At

I'm having Problems with tmux/screen in php.
When I try to launch a screen session nothing happens.
When I try to launch a tmux session it works only if I give www-data shell access and even then no windows are added :(

What I am trying to achieve:
I want to code a webinterface for some processes I run on a virtual machine.
For that I have written some scripts (in bash since I am on a linux debian machine).
These scripts need to do some things:
- start a new tmux window (and the session if it does not yet exist)
- see if a tmux window is running/active
- stop process (which automatically closes the window)
And executed manually (in a terminal) these scripts behave as wanted.

Now the webinterface shall start these scripts (so they get executed over the exec function in php).

Since they are run over php they are run as user www-data so I can't access tmux sessions created by www-data.

So i have a few recommendations when starting the Window / session:

  • I have a named session

    tmux new-session -s $SessionName

  • It needs to be started detached since its started from php

    tmux new-session -d -s $SessionName

  • I want to be able to access the session as superuser --> I have to define a socketlocation

    tmux -S $SocketLocationPath new-session -d -s $SessionName

So my command look like this in the end:

tmux -S /tmp/pairedsession.socket new-session -d -s "ProcessGroup1"

Problems with this command:

  • without shell access this command returns failed to connect to server (what is weird since new-session normally should start the server)

  • with shell access (what I don't like for security reasons) however the session is created exactly as I wanted

But there are still some Problems:

As I stated before I want to create some windows to handle the processes
The command looks like this:

tmux -S $SocketLocationPath new-window -t "$SessionName" -n "$WindowName" "$cmd"

This works but only for global processes (like htop) or processes in the php directory (like other scripts)
What I want to do though is execute a process in its own working directory which is not necessarily in a php directory --> HOW CAN I CHANGE THAT

What I tried

  • adding 2>&1 to every command for debugging --> no help
  • creating the sessions without the SocketLocation
  • switched from screen to tmux since it has easier syntax
  • gave www-data access to the shell

    sudo chsh -s /bin/sh www-data

  • using shell_exec, passthrough and exec functions in php

  • using screen with the -dmS option

Possible Solutions

  • give www-data shell access (Security Risk?) or maybe restrict the access to tmux but how?
  • disable phps setting to only execute files in php directories (Security Risk?)
  • add some to me unknown tmux option that works without shell :D
  • change some php setting to allow only certain directories or is that even worth it?

  • do something completely different ???

Another weird thing: when launching the tmux sessions over php the terminal has no user and no location
Normally it looks like user@machinename$ ...
With php it just looks like $ ... nothing more

I hope someone can help me, because I just dont like these messy workarounds :/

Thanks in advance - superfuzzy

1

There are 1 answers

0
Offenso On BEST ANSWER

After some struggling, with assistance googleguy@freenode#php i am finally beat this down.

It turns out that PHP 7.1 (in my case) have no such issue at all.

I am succeed on executing this sequence:

shell_exec("tmux new-session -s session_name -d");
shell_exec("tmux send -t session_name gedit ENTER");

Poke me it it not work with you