最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to start an entirely new process in node.js (not child)? - Stack Overflow

programmeradmin1浏览0评论

The only answers I've seen for starting a process is using something called child_process. But I want to spawn an entirely new process completely independent from my current running node process, is this possible?

The only answers I've seen for starting a process is using something called child_process. But I want to spawn an entirely new process completely independent from my current running node process, is this possible?

Share Improve this question asked Mar 11, 2014 at 19:55 Shai UIShai UI 51.9k77 gold badges217 silver badges316 bronze badges 1
  • 1 @AfshinMoazami, each process gets a copy of the environment variable at the startup. So the process will never refresh its environment variable? – Tarun Lalwani Commented Aug 24, 2019 at 11:03
Add a comment  | 

3 Answers 3

Reset to default 16

You can spawn a child process in a detached state, ignore the outputs, and remove the child from the parents event loop with child.unref().

This code will start someScript.sh, and exit while keeping someScript.sh running.

var spawn = require('child_process').spawn;

var child = spawn(__dirname + '/someScript.sh', [], {
    detached: true ,
    stdio: [ 'ignore', 'ignore', 'ignore' ]
});

child.unref();

For more detailed information and alternatives (such as logging output / etc), take a look at the documentation for spawn. There are other examples there as well:

http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Haven't got too much information but it seems like either spawn, exec or fork options for child process. The other option would be a shared port, both options are clarified further here: http://craigbrookes.com/2012/03/17/multiple-processes-in-nodejs/

You can use the natively provided child process facility: http://nodejs.org/api/child_process.html

And use the unix "nohup" command to keep the spawned process alive even if the parent process died.

发布评论

评论列表(0)

  1. 暂无评论