Skip to content

Instantly share code, notes, and snippets.

@David256
Created August 17, 2022 21:23
Show Gist options
  • Save David256/585d3f62306e043e33affc3cd9a26930 to your computer and use it in GitHub Desktop.
Save David256/585d3f62306e043e33affc3cd9a26930 to your computer and use it in GitHub Desktop.
express.js launches stuffs and responses if the process is still running
const express = require('express');
const {
exec,
} = require('child_process');
const app = express();
const port = 7000;
let isRunning = false;
app.get('/', (req, res) => {
// Response
res.json({
isRunning,
});
});
app.get('/start', (req, res) => {
if (isRunning) {
res.json({
message: 'it is running...',
});
return;
}
const child = exec('bash process.sh', (err) => {
if (err) {
isRunning = false;
}
});
isRunning = true;
child.on('close', () => {
isRunning = false;
});
child.on('exit', () => {
isRunning = false;
});
// Response
res.json({
message: 'starting...',
});
});
app.listen(port, () => {
// eslint-disable-next-line no-console
console.log(`launcher app listening on port ${port}`);
});
echo "working..."
sleep 30
echo "bye"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment