Skip to content

Instantly share code, notes, and snippets.

@timcash
Created November 4, 2022 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcash/97e41bfc4b6ec1eef85e8f4b0b007440 to your computer and use it in GitHub Desktop.
Save timcash/97e41bfc4b6ec1eef85e8f4b0b007440 to your computer and use it in GitHub Desktop.
spawn exec in js
// ========================================================
// 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