2

Does the following way

$ (sleep 123 &)
$ jobs
$ 

remove the process group of sleep 123 from bash's job control? What is the difference between the above way and disown then? Note that the sleep 123 process is still in the same process group led by the now disappearing subshell, and in the same process session as the interactive shell, so share the same controlling terminal.

Does not being in the shell's job control explain that the sleep 123 process will not receive any signal (including SIGHUP) sent from the bash process?

Thanks.

3
  • 3
    Possible duplicate of Putting subshell in background vs putting command in background
    – jesse_b
    Commented Dec 27, 2018 at 17:10
  • 2
    @Jesse_b Not subtle difference between (...)& in the proposed dupe and (...&) in this question.
    – Kusalananda
    Commented Dec 27, 2018 at 17:20
  • @Jesse_b this is absolutely not a dupe of the question you're pointing to. It may be rather a dupe of this.
    – user313992
    Commented Dec 28, 2018 at 6:20

1 Answer 1

2

Yes, it removes it, so to speak.

When you run a (...) subshell from an interactive bash script, a new process group (job) is created, which becomes the foreground process group on the terminal, and in the case where the subshell contains any command terminated by &, (eg. sleep 3600 &) that command will be started in the very same foreground process group, with SIGINT and SIGQUIT ignored and its input redirected from /dev/null). See here for some links to the standard.

When the (...) subshell exits, that foreground job is removed from the shell's jobs table, and the sleep 3600 command will continue to run outside of the control of the shell.

This is quite different from the case where (sleep 3600 &) is run from a non-interactive script with no job control, where everything (the main shell, its (...) subshell, and any "background" commands (foo &), inside or outside the (...) subshell) is run in the same process group.

2
  • Thanks. (1) pubs.opengroup.org/onlinepubs/9699919799/utilities/… doesn't say that "When the (...) subshell exits, that foreground job is removed from the shell's jobs table". How did you get it? (2) What is the difference between the above way and disown then?
    – Tim
    Commented Dec 27, 2018 at 19:19
  • 1) that link is about asynchronous lists; (...) is not such a thing, even if the subshell running in it may contain such async lists. That job will be removed from the table because its process group leader has exited when the prompt has been returned after the (...) command. 2) disown is a very bash specific thing, and will remove jobs from the list it will send a SIGHUP to upon exiting (that's different from the SIGHUP sent by the kernel); that sleep 123 & wasn't ever a separate job, as I already explained; the shell didn't keep tabs on it in the first place.
    – user313992
    Commented Dec 27, 2018 at 19:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .