Skip to content

Instantly share code, notes, and snippets.

@brampersandon
Created December 1, 2020 00:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brampersandon/58e148313c5a19e014e2b3c939cde11d to your computer and use it in GitHub Desktop.
Save brampersandon/58e148313c5a19e014e2b3c939cde11d to your computer and use it in GitHub Desktop.
Basic process monitor
const { spawn } = require("child_process");
const COMMAND = "ls"
const ALLOWED_REBOOTS = 10
async function main() {
console.log("Starting process");
let counter = 0;
let failed = false;
let handle;
start();
async function start() {
handle = spawn(COMMAND);
handle.stdout.on("data", (data) => {
console.log(data.toString("utf-8"));
});
handle.stderr.on("data", (data) => {
console.error(data.toString("utf-8"));
});
handle.on("close", (exitCode) => {
console.log(`Process exited with code ${exitCode}, restarting...`);
failed = true;
counter = counter + 1;
if (counter > ALLOWED_REBOOTS)
return console.log(
"Bailing after " + ALLOWED_REBOOTS + " restarts at " + new Date().toISOString()
);
start();
});
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment