Sample Header Ad - 728x90

How to read server stdout and continue only after message is outputted

1 vote
2 answers
300 views
Say I have a simple Node.js server like:
const http = require('http');

const server = http.createServer((req,res) => res.end('foobar'))

server.listen(3000, () => {
   console.log(JSON.stringify({"listening": 3000}));
});
and then with bash: #!/usr/bin/env bash node server.js | while read line; do if [[ "$line" == '{"listening":3000}' ]]; then : fi done # here I want to process more crap my goal is to only continue the script after the server has started actually listening for requests. The best thing I can come up with this this: #!/usr/bin/env bash mkfifo foo mkfifo bar (node server.js &> foo) & ( while true; do cat foo | while read line; do if [[ "$line" != '{"listening":3000}' ]]; then echo "$line" continue; fi echo "ready" > bar done done ) & cat bar && { # do my thing here? } is there a less verbose/simpler way to do this? I just want to proceed only when the server is ready and the only good way I know of doing that is to using stdout to print a message and listen for that.
Asked by user393961
Apr 30, 2020, 04:55 AM
Last activity: Apr 30, 2020, 02:41 PM