const { spawn } = require('child_process');
var child = spawn('sleep', ['10s']);
var kill = spawn('kill', [child.pid]);
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
child.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
child.on('close', (code) => {
console.log(`子进程退出,退出码 ${code}`);
});
结果:
子进程退出,退出码 null
杀死后重启
const { spawn } = require('child_process');
var child = spawn('sleep', ['10s']);
var kill = spawn('kill', [child.pid]);
child.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
child.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
child.on('close', (code) => {
console.log(`子进程退出,退出码 ${code}`);
child = spawn('sleep', ['10s']);
});
结果:
子进程退出,退出码 null
10s 后退出