I am starting entr as a subshelled background process as described in its man page to listen to changes to some file foo, occuring during interactive use of some program baz, and I would like to terminate that background process on completion of baz: (echo foo | entr echo bar &) && baz;

I found this question which tries to get the process id of a background process started in this way, which seems very convoluted to do in bash. Is there some way to kill it?

  • Why are you using a (...) subshell in the firstplace? (also notice that the && is misleading, the exit status of (... &) is always zero). Why not just echo foo | entr echo bar & baz; kill $!? – mosvy Oct 16 at 14:18
  • I figured out my problem: entered into a tty, you need a subshell to circumvent interactive mode. In the script I ended up not needing it. – Commodore64 Oct 17 at 8:53
  • The manpage you link to describes a -n option for non-interactive mode -- but I have not used that entr utility. Anyways, a more reliable and non-racy way to run a command with no /dev/tty on Linux is with setsid command ..., rather then exploiting the quirks of the subshell implementation (which depend from one shell to another). – mosvy Oct 17 at 9:07
  • Wow that works too! Thanks for pointing that out. I'm a bit of a bash novice and I'm very grateful for this kind of advice that goes beyond the problem at hand. : ) – Commodore64 Oct 17 at 10:25

Your Answer

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

Browse other questions tagged or ask your own question.