0

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?

  • 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
  • 2
    @nohillside no, sleep is interruptible by non-ignored signals. – Stephen Kitt Dec 22 '18 at 17:44
2

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).

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.