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?

  • capture the output where? In a file, shell variable? Jul 29, 2017 at 17:34
  • file, variable, doesn't matter Jul 29, 2017 at 17:35

1 Answer 1

(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 before unbuffer if available as unbuffer has a few issues. Jul 29, 2017 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.