0

So I would like to run a script which kills my nodejs process and runs another one in another screen window. When I run the command from the console it works, but when run from nodejs script the kill part gets executed but the server is never run.

filelog('Attempting restartNodejs with PID: ' + pid
    + ' serverjsLocation: ' + serverjsLocation);
filelog('kill ' + pid + ';screen -d -m -L nodejs ' + serverjsLocation);
//return;

exec('kill ' + pid + ';screen -d -m -L nodejs ' + serverjsLocation);

and the log is

Attempting restartNodejs with PID: 29685 serverjsLocation: /home/git/Gitorade/server.js
kill 29685;screen -d -m -L nodejs /home/git/Gitorade/server.js

but the server is not run. ps -e | grep nodejs returns nothing while at the first run it does output the process.

My guess would be that it's run as a fork so somehow when I kill the nodejs the screen command never gets executed because I can in no way get any response or log out of the action. The screen.0 log is empty after that command.

How do I run this exec independently so that it does not get killed after kill ?

ditoslav
  • 4,563
  • 10
  • 47
  • 79
  • How about 2 exec calls, one for kill and one for screen? – Kenney Oct 22 '15 at 15:22
  • Wouldn't the kill stop the screen from executing? (since im killing the very same process from which this is called) – ditoslav Oct 22 '15 at 15:23
  • Not if you run it first. I am not sure, hence no answer, but I don't think `exec` supports multiple commands (the `;`). – Kenney Oct 22 '15 at 15:23
  • I can't run the screen first because of something else. I have to kill the first nodejs process to start the next one – ditoslav Oct 22 '15 at 15:27
  • I'm sorry for being unclear. This is what I meant: `exec('kill 29685'); exec('screen -d ...')`. – Kenney Oct 22 '15 at 15:30
  • I will try but wont the kill prevent the program from moving to the next exec? – ditoslav Oct 22 '15 at 15:59
  • Ah I get it now. If you want to terminate the current process you could simply [exit](http://stackoverflow.com/questions/5266152/how-to-exit-in-node-js). I haven't found anything for nodejs like a [shell `exec`](http://stackoverflow.com/questions/18351198/what-are-the-uses-of-the-exec-command-in-shell-scripts), which terminates the current process and continues as the exec'd process. Could you resolve this in your startup script with a simple loop, starting the process again if it terminates? – Kenney Oct 22 '15 at 16:07

0 Answers0