所以我需要一种方法来重新启动我的机器人,并最终解决了这个问题,但是当使用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 $