1

In bash, when running ( sleep 123 &), the sleep 123 process will continue running, when the subshell exits. How can I stop the sleep 123 process before its parent subshell exits?

I'm trying to see if the sleep 123 process will be terminated, because of receiving SIGHUP and SIGCONT. I am looking for an example for Is SIGHUP sent to this orphaned process, and why doesn't it terminate? and Does kernel sending SIGHUP to a process group that becomes orphaned and contains a stopped process terminate all the processes by default?

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
Tim
  • 98,580
  • 191
  • 570
  • 977
  • 1
    Is `sleep` just an example, or is this your real code? Because `sleep` (the binary) basically calls `sleep` (the kernel function) which means it only processes signals once time is up. – nohillside Dec 22 '18 at 16:45
  • Thanks. Do you mean `sleep` is not stoppable by signal? you can use whatever command that can be stopped. – Tim Dec 22 '18 at 17:01
  • Processes can't be stopped while they run in kernel mode. `sleep 123` basically makes a syscall to `sleep(123)` which only will return once time has run out. So a real life example would call either another shell script or a non-sleeping binary inside the `(...&)`. – nohillside Dec 22 '18 at 17:08
  • Thanks. If a "non-sleeping binary" makes system calls and a signal tries to stop the process while the system call is running, will it be stopped only after the system call returns to user space code? – Tim Dec 22 '18 at 17:14
  • Exactly. Which may or may not be a problem, depending on your scenario – nohillside Dec 22 '18 at 17:40
  • Thanks. Still how can I stop a stoppable command in a subshell before the subsehll exits? – Tim Dec 22 '18 at 17:41
  • 3
    @nohillside no, `sleep` is interruptible by non-ignored signals. – Stephen Kitt Dec 22 '18 at 17:44
  • Do you want to make the command stop, or do you want the subshell to wait until it completes? – William Pursell Dec 22 '18 at 18:18
  • I see you rolled back an edit, that made your words make sense. Did this edit misrepresent what you were saying, or is it like on other questions, that you just don't like help? Can you help us to edit your question so that it has a meaning, at present the tile is word salad, and I can not see any alternate meaning. – ctrl-alt-delor Dec 22 '18 at 19:15
  • @ctrl-alt-delor Sorry, it is "stopped" not "exiting". Check out the links I gave, for details. – Tim Dec 24 '18 at 18:05
  • Why do you want to stop (pause) process, went the process exits. It seems like you will have a lot of stopped processes, hanging around. – ctrl-alt-delor Dec 24 '18 at 19:26

1 Answers1

3

This will show the behaviour you’re trying to illustrate:

(sleep 60 & kill -STOP $!)

This puts sleep in the background, then stops it. It then gets killed by SIGHUP when the subshell exits.

Signals can interrupt some system calls; see the signal(7) manpage (in particular the “Interruption of system calls and library functions by signal handlers” section). The system calls used by sleep in particular are interrupted when a signal handler is invoked, and this is documented in sleep(3).

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164