Created
November 4, 2022 00:28
-
-
Save timcash/97e41bfc4b6ec1eef85e8f4b0b007440 to your computer and use it in GitHub Desktop.
spawn exec in js
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
// ======================================================== | |
// REFERENCE | |
// ======================================================== | |
async function spawnApplication({ log, ctx, is }) { | |
const startPath = "./src/start.js"; | |
const childServer = spawn(`node`, [startPath, "8083", "ram", "write"]); | |
childServer.stdout.on("data", (data) => { | |
const str = data.toString().trim(); | |
log(ll.info, "SPAWN:", str); | |
}); | |
childServer.stderr.on("data", (data) => { | |
log(ll.alert, "SPAWN:", data.toString()); | |
}); | |
const p = new Promise((resolve, reject) => { | |
childServer.on("close", (code) => { | |
log(ll.info, "SPAWN", `child process close all stdio with code ${code}`); | |
resolve(code); | |
}); | |
childServer.on("exit", (code) => { | |
log(ll.info, "SPAWN", `child process exited with code ${code}`); | |
resolve(code); | |
}); | |
}); | |
return p; | |
} | |
async function killServer(port) { | |
try { | |
const cmd = `lsof -i:${port} | grep LISTEN | awk '{print $2}'`; | |
const { stdout } = await execP(cmd); | |
const pid = stdout.trim(); | |
await execP(`kill -9 ${pid}`); | |
log(ll.alert, "KILLED", pid); | |
} catch (error) { | |
log(ll.alert, "KILL", "PORT OPEN"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment