Reputation: 724
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.
Upvotes: 3
Views: 4911
Reputation: 23515
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}`);
});
Upvotes: 3