Running a command inside tmux session using PHP

Viewed 226

I am trying to start a nodejs server inside a tmux session using PHP with this code:

print_r(shell_exec("tmux send -t session1.0 ./start.sh ENTER 2>&1"));

This code returns: no server running on /tmp/tmux-33/default. I have created this session (session1) using tmux new -s session1 with root account. When I try to start the session using php (print_r(shell_exec("tmux new -s worker1 2>&1"));), this returns open terminal failed: not a terminal

How do I start a nodejs server using PHP?

1 Answers

Tmux is working per user on unix so basically you have to create that terminal on the user which you are use while running the script. So if its run by webserver it may be www-data. If you run the script from CLI just use current user not root.

See example:

<?php 
print_r(shell_exec('tmux ls')); // print all sessions

//shell_exec('tmux new -s example'); // you can even create session directly from php

shell_exec('tmux send -t example.0 ls ENTER'); //send the commend `ls`
Related