Node.js child process with detached option

邮差的信 提交于 2019-12-11 07:54:30

问题


I am creating an electron desktop app, and I have code use spawn() with option detached: true. My purpose is to let the child process keep running even when the parent process terminated.

const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr'], { detached: true });

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
  fs.writeFileSync('path-to-test.txt', 'stdout');
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
  fs.writeFileSync('path-to-test.txt', 'stderr');
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

squirrel events https://github.com/electron/grunt-electron-installer#handling-squirrel-events:

  switch (squirrelCommand) {
    case '--squirrel-install':
    case '--squirrel-updated':
      app.quit();

      return true;
    case '--squirrel-uninstall':
      app.quit();
      return true;
    case '--squirrel-obsolete':
      return true;
  }

I tested the above code outside of squirrel events, it works well when the parent process is alive. But after I put these code inside squirrel events like --squirrel-uninstall (the parent process may terminated before/during child process run), it can only run commands, any code inside it (like fs function) doesn't work any more.

My have a question is: despite of squirrel event, can the logic code like fs inside child process work after the node parent process terminate?

来源:https://stackoverflow.com/questions/39577012/node-js-child-process-with-detached-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!