duanhe4155 2015-03-27 13:40
浏览 44
已采纳

php使用exec与屏幕

I have a need to start three processes through my PHP script. Those processes live in the /home/user/server directory, named server-one, server-two and server-three. The process are also capable of accepting commands through their consoles, so I would need them to live in their own screen with a name so I can use -X stuff to give them commands. As well, each of the processes gives status information and debug information through stdout on their consoles, so I want to log the output to display in case I ever need to check up on the process and what the last debug was through my console

So, in other words, I need to start a process in a named screen, output the stdout console information log to a file, as well as have the ability to send commands directly to the screen if I need.

I have the PHP all below, in one file:

 <?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
'", $serverPath));
 exec(sprintf("screen -S server-one -X stuff $'./%s > %s
'", $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
'", $serverPath));
 exec(sprintf("screen -S server-two -X stuff $'./%s > %s
'", $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
'", $serverPath));
 exec(sprintf("screen -S server-three -X stuff $'./%s > %s
'", $serverThreeExec, $threeOut));
 ?>

The screens are all created correctly, but the servers are not started. As a debug test I changed all the exec commands to echo what the sprintf is passing to exec and it is definitely correct. As a test I ran the commands in SSH and they executed properly and had the desired effect, so it's not anything in the actual command as outputted that's causing trouble.

All the permissions to the executables and logs are correct as the log files are created, but the log file is not filled with anything, it's empty. I can't access the screens directly as the user where php is running is www-data and you can't login to www-data through SSH. If I run the server through the web browser without screen and just throw the process into the background, it works correctly, so not a permission issue there, either, but I need those processes to be in a screen so I can manipulate them later through their consoles.

Am I missing anything here?

  • 写回答

1条回答 默认 最新

  • doukang2003 2015-03-29 06:54
    关注

    I've solved this by removing the $'' around the command to send to the screen and instead used \"\" with the surrounded in ticks.

    So, for example, changed:

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

    ...to....

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

    And it now works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献