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

javascript - How to tell if child Node.js Process was from fork() or not? - Stack Overflow

programmeradmin1浏览0评论

I have a small application that could be executed by a fork or directly by a developer, and I would for it to get configured slightly differently depending on how it was started.

I know that I could always pass in arguments to to signal that it was a fork, but I was just curious if there was a way to tell if I could somehow know in the child process if it came from a fork(). I looked around in process but didn't find anything telling.

I have a small application that could be executed by a fork or directly by a developer, and I would for it to get configured slightly differently depending on how it was started.

I know that I could always pass in arguments to to signal that it was a fork, but I was just curious if there was a way to tell if I could somehow know in the child process if it came from a fork(). I looked around in process but didn't find anything telling.

Share Improve this question asked Jun 10, 2015 at 17:18 Hyo ByunHyo Byun 1,27610 silver badges19 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 29

You can check if process.send exists in your application. When it was started using fork() it will exist.

if (process.send === undefined) { 
  console.log('started directly');
} else {
  console.log('started from fork()');
}

Personally, I would probably set an environment variable in the parent and check for that in the child:

// parent.js
child_process.fork('./child', { env : { FORK : 1 } });

// child.js
if (process.env.FORK) {
  console.log('started from fork()');
}
发布评论

评论列表(0)

  1. 暂无评论