I am using child_process.spawn for running some app and I want to execute some actions after this app was started. But spawn blocks main process
const spawnAsync = Promise.promisify(require('child_process').spawn);
console.log('BEFORE');
await spawnAsync('../node_modules/.bin/some-app', ['-p', '3333'], {
detached: true
});
console.log('AFTER');
In my console, I see BEFORE and AFTER would be logged only after killing process on 3333 port. I don't want to wait spawned process, I want to do some actions after spawnSync calling.
Looking at spawn documentation you should use it that way
const childProcess = require('child_process');
const newProcessDescriptor = childProcess.spawn(
'../node_modules/.bin/some-app',
['-p', '3333'],
{ detached: true }
);
newProcessDescriptor.on('error', (error) => {
console.log(`child process creating error with error ${error}`);
});
newProcessDescriptor.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With