I need to run a certain command (ngrok in this case) for a few seconds, capture all the output, filter out some variables and then detach the process, making it run invisibly in the background. How can I do that?
1 Answer
(cmd &) | (timeout --foreground 2 cat; cat > /dev/null &)
Would show you the output of cmd
for 2 seconds and then return, with cmd
running in background with its output going to /dev/null
.
You can redirect that to a file or store in a variable with:
var=$(that command)
-
ngrok output can't unfortunately get captured like this, because it's sort of like apt output line updating, which doesn't get captured in a file. So this solution doesn't output anything. Thanks anyway! Jul 29, 2017 at 18:09
-
I was able to make it work using this solution when I disabled the buffering via the expect's unbuffer. Thanks! Jul 29, 2017 at 18:15
-
@freemanovec, you may try to use
stdbuf -o0
beforeunbuffer
if available asunbuffer
has a few issues. Jul 29, 2017 at 18:36