So I needed a way to restart my bot within itself and came up with this eventually, however when testing on a Raspberry Pi using ssh it exits after the first child process ends, whereas on Windows it continuously repeats this cycle
Run using node 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)
Output on 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...
Output on the 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.
From this answer
Changing the exit code to 2 made it work