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.

Tim
  • 101,790

1 Answers1

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.

  • Thanks. (1) http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_03_02 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 Dec 27 '18 at 19:19
  • 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.
  • –  Dec 27 '18 at 19:40