【问题标题】:php using exec with screenphp使用带屏幕的exec
【发布时间】:2015-03-27 13:40:15
【问题描述】:

我需要通过我的 PHP 脚本启动三个进程。这些进程位于/home/user/server 目录中,名为server-oneserver-twoserver-three。该进程还能够通过他们的控制台接受命令,所以我需要他们住在他们自己的屏幕上并命名,这样我就可以使用-X stuff 给他们命令。同样,每个进程都通过其控制台上的标准输出提供状态信息和调试信息,所以我想记录输出以显示以防我需要检查进程以及最后一次调试是通过我的控制台进行的

所以,换句话说,我需要在命名屏幕中启动一个进程,将标准输出控制台信息日志输出到一个文件中,并且如果我需要能够直接向屏幕发送命令。

我将 PHP 全部放在一个文件中:

 <?php
 exec('screen -dmS server-one');

 $serverPath = "/home/user/server";

 $oneOut = "/var/www/log/server-one.log";
 $serverOneExec = "server-one";
 exec(sprintf("screen -S server-one -X stuff $'cd %s\n'", $serverPath));
 exec(sprintf("screen -S server-one -X stuff $'./%s > %s\n'", $serverOneExec, $oneOut));

 $screentwo = "screen -dmS server-two";
 exec($screentwo);
 $twoOut = "/var/www/log/server-two.log";
 $serverTwoExec = "server-two";
 exec(sprintf("screen -S server-two -X stuff $'cd %s\n'", $serverPath));
 exec(sprintf("screen -S server-two -X stuff $'./%s > %s\n'", $serverTwoExec, $twoOut));

 $screenthree = "screen -dmS server-three";
 exec($screenthree);
 $threeOut = "/var/www/log/server-three.log";
 $serverThreeExec = "server-three";
 exec(sprintf("screen -S server-three -X stuff $'cd %s\n'", $serverPath));
 exec(sprintf("screen -S server-three -X stuff $'./%s > %s\n'", $serverThreeExec, $threeOut));
 ?>

所有画面均已正确创建,但服务器未启动。作为调试测试,我更改了所有 exec 命令以回显 sprintf 传递给 exec 的内容,这绝对是正确的。作为测试,我在 SSH 中运行了这些命令,它们正确执行并达到了预期的效果,因此输出的实际命令中没有任何东西会导致问题。

在创建日志文件时,对可执行文件和日志的所有权限都是正确的,但日志文件没有填充任何内容,它是空的。我无法直接访问屏幕,因为运行 php 的用户是 www-data,您无法通过 SSH 登录到 www-data。如果我通过不带屏幕的 Web 浏览器运行服务器并将进程放入后台,它可以正常工作,因此也不是权限问题,但我需要将这些进程放在屏幕中,以便稍后通过他们的控制台。

我这里有什么遗漏吗?

【问题讨论】:

    标签: php linux exec gnu-screen


    【解决方案1】:

    我已经通过删除命令周围的$'' 来解决这个问题以发送到屏幕,而是使用\"\"\n 括在刻度中。

    所以,例如,改变了:

     exec(sprintf("screen -S server-three -X stuff $'./%s > %s\n'", $serverThreeExec, $threeOut));
    

    ...到....

     exec(sprintf("screen -S server-three -X stuff \"./%s > %s\"'\n'", $serverThreeExec, $threeOut));
    

    现在它可以工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多