dongshu9458 2017-05-23 08:42
浏览 43
已采纳

通过PHP在屏幕会话中启动节点的麻烦

So to start; I am trying to launch a node application via PHP. I created a script that can check if the node application is running, close it and start it. But I have some trouble with using screen with PHP.

Now before I carry on, comments about how I shouldn't do it won't help, I need to have it launched via PHP, due to it working alongside a website, and I want our client to be able to change settings, which will relaunch the application (unless you have another idea).

I am currently using the command:

screen -dmS NODEJS node main.js 121016

Now I can launch node, and get the output (as long as I add a process.exit somewhere so PHP/Node doesn't run forever). I am already in the correct directory also, I am using this code to launch it:

$out = shell_exec('screen -dmS NODEJS node main.js 121016');
var_dump($out);

But it is not creating the screen session. I have also tried:

$proc = proc_open('screen -dmS NODEJS node main.js 121016', $this->pipe_spec, $pipes);

I also tried to use exec and the back ticks but I am fairly certain the back ticks are the same as shell_exec?

Anyway I am completely lost, and am not sure what else I can do. I don't want node constantly running, which is why I want it to launch when you click save in the manager (in php), and then it closes/stops itself when it has completed it's tasks.

Any help would go a long way, I have spent hours trying to work it out, but I am getting no where... thanks anyway.

  • 写回答

1条回答 默认 最新

  • dongzhi9574 2017-11-07 10:41
    关注

    An update on this for anyone interested.

    The issue was caused because I wasn't managing the pipes correctly, and was not sending the stdOut pipe. The problem occurred mainly when I tried to remove screen as it was not giving me the correct PID.

    An update on what I used in the end:

    node main.js > /dev/null 2> logs/app-error.log & echo $!
    

    The last one '2>' I was not managing correctly.

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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能