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?