Created
December 1, 2020 00:48
-
-
Save brampersandon/58e148313c5a19e014e2b3c939cde11d to your computer and use it in GitHub Desktop.
Basic process monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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