【问题标题】:Running a command inside tmux session using PHP使用 PHP 在 tmux 会话中运行命令
【发布时间】:2021-03-08 20:12:14
【问题描述】:

我正在尝试使用 PHP 和以下代码在 tmux 会话中启动 nodejs 服务器:

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

此代码返回:no server running on /tmp/tmux-33/default。我使用tmux new -s session1 和root 帐户创建了这个会话(session1)。当我尝试使用 php (print_r(shell_exec("tmux new -s worker1 2>&1"));) 启动会话时,这将返回 open terminal failed: not a terminal

如何使用 PHP 启动 nodejs 服务器?

【问题讨论】:

    标签: javascript php node.js session background


    【解决方案1】:

    Tmux 在 unix 上按用户工作,因此基本上您必须在运行脚本时使用的用户上创建该终端。所以如果它由网络服务器运行,它可能是www-data。如果您从 CLI 运行脚本,只需使用当前用户而不是 root。

    查看示例:

    <?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`
    

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 2014-12-27
      • 2012-01-21
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      • 1970-01-01
      相关资源
      最近更新 更多