第二个节点子进程在 linux 上 运行 失败
2nd node child process fails to run on linux
所以我需要一种方法来重新启动我的机器人并最终想出了这个方法,但是当使用 ssh 在 Raspberry Pi 上进行测试时,它会在第一个子进程结束后退出,而在 Windows 不断重复这个循环
运行 使用节点 run.js
run.js
const { spawn } = require('child_process');
var bot;
function startBot() {
bot = spawn('node', ["bot.js"]);
bot.stdout.on('data', data => console.log(data.toString()));
bot.stderr.on('data', data => console.error(data.toString()));
bot.on('close', code => {
if (code === 1234) startBot();
});
}
startBot();
bot.js
setTimeout(() => {
console.log("child process exiting...");
process.exit(1234);
}, 5000)
Windows 上的输出:
PS C:\Users\[removed]\Desktop\despacito\despacito-spider\test> node run.js
child process exiting...
child process exiting...
child process exiting...
child process exiting...
child process exiting...
Raspi 上的输出
pi@raspberrypi:~/Desktop/despacito-spider/test $ node run.js
child process exiting...
pi@raspberrypi:~/Desktop/despacito-spider/test $
It is very rare for programs to exit with status codes greater than 128, in part because programmers avoid it due to the $? ambiguity.
来自this回答
将退出代码更改为 2 使其工作
所以我需要一种方法来重新启动我的机器人并最终想出了这个方法,但是当使用 ssh 在 Raspberry Pi 上进行测试时,它会在第一个子进程结束后退出,而在 Windows 不断重复这个循环
运行 使用节点 run.js
run.js
const { spawn } = require('child_process');
var bot;
function startBot() {
bot = spawn('node', ["bot.js"]);
bot.stdout.on('data', data => console.log(data.toString()));
bot.stderr.on('data', data => console.error(data.toString()));
bot.on('close', code => {
if (code === 1234) startBot();
});
}
startBot();
bot.js
setTimeout(() => {
console.log("child process exiting...");
process.exit(1234);
}, 5000)
Windows 上的输出:
PS C:\Users\[removed]\Desktop\despacito\despacito-spider\test> node run.js
child process exiting...
child process exiting...
child process exiting...
child process exiting...
child process exiting...
Raspi 上的输出
pi@raspberrypi:~/Desktop/despacito-spider/test $ node run.js
child process exiting...
pi@raspberrypi:~/Desktop/despacito-spider/test $
It is very rare for programs to exit with status codes greater than 128, in part because programmers avoid it due to the $? ambiguity.
来自this回答
将退出代码更改为 2 使其工作