前景孩子
运行一童子如来前行相. 给它stdio。 退出 当它离开.
主要是这个模块是用来支持 周围的一些案件
将儿童流程包成测试范围。 不过
也可以在您想要一个程序执行时普遍有用
另一段仿佛是“主要”过程,例如,如果一个程序
使用一个 --cmd 参数以某种方式执行.
用户
import { foregroundChild } from 'foreground-child'
// hybrid module, this also works:
// const { foregroundChild } = require('foreground-child')
// cats out this file
const child = foregroundChild('cat', [__filename])
// At this point, it's best to just do nothing else.
// return or whatever.
// If the child gets a signal, or just exits, then this
// parent process will exit in the same way.
您可以通过在之后传递对象提供自定义的产卵选项 程序和参数:
const child = foregroundChild(`cat ${__filename}`, { shell: true })
如果您想要执行 在您的前景孩子退出前动作 :
const child = foregroundChild('cat', [__filename], spawnOptions, () => {
doSomeActions()
})
回调可以返回一个承诺来履行 一个同步动作。 如果召回不回一个承诺, 然后它必须在 JavaScript 中完成行动 勾选.
const child = foregroundChild('cat', [__filename], async () => {
await doSomeAsyncActions()
})
如果召回投出或拒绝,那么它就会被解开 节点会出错 .
如果回调值返回字符串值,则该值将作为
退出父进程的信号。 如果它返回一个数字,
然后,该数字将作为父退出状态代码。 若为
返回布尔值 false,则父进程将不会是
终止。 如果它回来了 undefined,然后它会离开 与
和孩子的过程相同.
洞穴
"正常"标准 IO 文件描述符 (0, 1和 Stdin, 2), stdout和stderr分别与儿童过程共享. 此外,如果在父母中设置了IPC频道,那么 信件在文件描述符 3 中被代理给孩子 .
在节点中,也可以绘制任意文件描述符 进入一个儿童过程。 在这种情况下,未来孩子不会 将文件描述符映射到孩子中。 如果文件描述符 0, 1或2用于IPC频道,然后奇怪的行为可能 发生( 例如将 IPC 信件打印到 stderr) .
注意 SIGKILLL 总是会杀死母进程, 但是 将不会代理给孩子进程的信号, 因为 SIGKILLL 无法被抓住。 为了解决这个问题,一个特殊的"监视" 产出一个孩子的过程, 将发送一个SIGKILLL给孩子 进程,如果该程序在 监督员由于父母终止而收到SIGHUP.
在 Windows 上,发布一个 process.kill(process.pid, signal) 带有
致命的终止信号可能导致进程以 1
状态代码而不是正确报告信号。 这个
模块尝试做正确的事情,但是在Windows系统中,您
可能会看到错误的结果。 据我所知没有
为这个工作.
用户位 : foreground-child/proxy-signals
如果您只是想要代理给一个孩子过程的信号
主进程接收, 您可以使用 proxy-signals 导出
从这个包裹.
import { proxySignals } from 'foreground-child/proxy-signals'
const childProcess = spawn('command', ['some', 'args'])
proxySignals(childProcess)
现在,任何致命的信号 通过当前进程收到 亲接儿童过程.
它不会朝另一个方向走; i,信号发送到
儿童过程不会影响父母。 为了这个,听
儿童 exit 或者说 close 并妥善处理.
用户位 : foreground-child/watchdog
如果你正在产下一个孩子的过程, 想确保它 如果父进程退出, 您可以使用 此模块导出的监督工具 .
import { watchdog } from 'foreground-child/watchdog'
const childProcess = spawn('command', ['some', 'args'])
const watchdogProcess = watchdog(childProcess)
// watchdogProcess is a reference to the process monitoring the
// parent and child. There's usually no reason to do anything
// with it, as it's silent and will terminate
// automatically when it's no longer needed.