Created
August 17, 2022 21:23
-
-
Save David256/585d3f62306e043e33affc3cd9a26930 to your computer and use it in GitHub Desktop.
express.js launches stuffs and responses if the process is still running
This file contains hidden or 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 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}`); | |
}); |
This file contains hidden or 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
echo "working..." | |
sleep 30 | |
echo "bye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment