Home > database >  Can Bash register TSTP/CONT traps for its own "suspend" command?
Can Bash register TSTP/CONT traps for its own "suspend" command?

Time:09-15

I suspect the answer is 'no', but I wanted to rule out the possibility that there is a way and I'm just missing some clever syntax.

The problem boils down to

outer$  bash
inner$  trap 'echo TSTP received' TSTP
inner$  trap 'echo CONT received' CONT
inner$  suspend

[1]   Stopped                 bash
outer$  %1
bash

inner$  ah well shucks
bash: ah: command not found

I can already register a trap for EXIT in the inner shell with no difficulties. Being able to run short commands when receiving the TSTP and CONT signals would be such an elegant solution to the new issues that have come up, but of course stop/resume handling in job control is significantly different than simply running a cleanup handler when exiting...

Can trap be made to work in this way?

CodePudding user response:

Short answer, no. suspend apparently uses an actual SIGSTOP (not SIGTSTP), which I believe (like SIGKILL) cannot be caught - someone please correct me if I'm wrong. Your trap for SIGTSTP is working fine, though. :)

Using your outer$ and inner$ "prompts" to indicate state and hopefully reduce confusion, though obviously nothing is setting them that way. My prompts were different enough to be clear, but too noisy to use here.

outer$ bash
inner$ trap 'echo TSTP received' TSTP # this catches - and effectively ignores - TSTP
inner$ kill -TSTP $$ # this raises a TSTP to the current process
TSTP received
inner$ suspend # this is apparently not sending a TSTP

[1]   Stopped                 bash

outer$ fg 1 # this resumes the suspended job back into foreground
inner$ echo $$
1402
inner$ suspend

[1]   Stopped                 bash

outer$ kill -CONT 1402 # this also wakes it up
inner$ 
  • Related