0

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.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dahknee
  • 591
  • 3
  • 12
  • 28
  • Why are you even trying to use screen? – Jonnix May 23 '17 at 08:44
  • Just use `exec('node main.js &');` – Philipp May 23 '17 at 08:50
  • @JonStirling I am using screen, so that I can connect to the program in case anything goes wrong, due to it running alongside a website. Have you got a better way to do it? that allows me to send it commands also to stdin? As I need to send it commands also! – Dahknee May 23 '17 at 08:54
  • Hi @Philipp will this work on server hosting? I think they disable exec, would shell_exec work? – Dahknee May 23 '17 at 08:55
  • If they allow `shell_exec`, they also allow `exec` - but in case they don't, just replace `exec` with `shell_exec` – Philipp May 23 '17 at 09:34
  • Thanks @Philipp, I tried using this, but how can I control the application? As in interact with it on a console level? Currently it can take commands, like sending :shutdown via stdin, which will safely close it? Is there a way to do this with your method? – Dahknee May 23 '17 at 09:48
  • You could stop the application with it's pid. To get the pid, you could run nginx like this `$pid = shell_exec("nohup nginx main.js > /dev/null 2>&1 & echo $!");` Using the pid, you could send any signal to the process. I.e. kill it `shell_exec("kill $pid");` – Philipp May 23 '17 at 14:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144944/discussion-between-danny-smc-and-philipp). – Dahknee May 23 '17 at 14:13

1 Answers1

1

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.

Dahknee
  • 591
  • 3
  • 12
  • 28