Sample Header Ad - 728x90

Launching a program via `xdg-open` from a subshell without blocking

0 votes
1 answer
2737 views
I've noticed that calling xdg-open from a subshell will reliably block until the launched process is closed. I suspect there may be a reason for this, but I'm not sure as to why. For example, launching Nautilus doesn't block when calling xdg-open directly from the command line: xdg-open ~/dir ; echo foo # doesn't block but invoking xdg-open from a subshell will reliably block the terminal var=$(xdg-open ~/dir ; echo foo) # blocks { xdg-open ~/dir ; echo foo ; } | cat # blocks. My understanding is that xdg-open detaches the launched process from the shell session so that it's no longer a subprocess. I'd therefore expect this to be different to e.g. invoking sleep 1 & in a subshell for which it seems reasonable that the terminating subshell will block until all subprocess have completed, i.e. var=$(sleep 1 & echo foo) # also blocks, but understandable. But if xdg-open is detaching the process, what's causing the subshell to wait? In what may (?) be a partial answer, I've noticed that running { xdg-open ; ps ; } | cat shows that depending on program launched by ``, those that block are also the ones that keep the tty as their controlling terminal. That begs the question why this happens, why this happens only in a subshell and ultimately what's a good way to a launch desktop process from the terminal that will fully and reliably detach from it? Edit: fix syntax on bash.
Asked by wardw (396 rep)
Dec 12, 2020, 08:08 PM
Last activity: May 14, 2025, 12:06 AM