Вопрос

I have a nodejs server that executes commands through child_process.exec. One such command restarts the node instance (and a couple of other things).

The script does something like this:

  • kill node
  • kill programs node uses
  • clear logs
  • restart programs node uses
  • restart node

I know this is not the best way of doing it, but my hands are tied (it's a released version of software and we can't restructure until the next release).

What's happening is that when I run the command, it kills node and then the rest of the script dies.

The default shell that node is using is sh, and that cannot be changed.

What are my options?

The server is running as root, so there won't be any permissions problems. I figure I'll have to start a new shell somehow, but I'm not entirely sure how to do that. I'd like to exit the current shell successfully, but that isn't a requirement, as long as I can get the an entire script separate from the current shell.

Это было полезно?

Решение

After much toil and heartbreak, this is the pattern of what succeeded:

  • echo script > /dev/shm/filename
  • chmod +x /dev/shm/filename
  • nohup /dev/shm/filename

Then in the script:

killall node

This is a pretty low-tech way of solving the problem and of course not optimal, but it works. Since nohup was used, it will not die when the parent dies. This means that all of the scripts will run.

It also turned out that I had another error that complicated things, which has since been fixed.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top