3

I want to trap the SIGTSTP signal, but I'm not able to. When I call just trap command without arguments, I'm getting this:

trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU

Obviously, my trap has some already pre-defined behaviour. The problem is, I'm not able to rewrite it, even when I'm logged as a root. When I use the trap command, it's simply ignored for these three signals. Otherwise it works normally.

I tried to rewrite it just by command, like that:

trap -- 'echo SIGTSTP' SIGTSTP

The other strange thing is, I'm able to use this signals and they're working normally, they're not ignored. I can simply use SIGTSTP to stop working some other process and make it continue with SIGCONT again.

So, do you know, how to rewrite those commands? And could you, please, explain this strange behaviour a little?


My OS is Lubuntu 15.04 and I'm using GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu).

  • Similarly, to demonstrate this problem in a simpler form, I notice that my shell is effectively hung if I run "(trap : TSTP;sleep 5)" and press ctrl-z. After 5 seconds, the sleep does not complete. Ctrl-c no longer works which doesn't make sense. Before pressing ctrl-z, ctrl-c works fine. – Curtis Yallop Jan 10 '18 at 17:56
  • Interestingly, if I run "(trap 'echo stop' SIGTSTP; trap; read)", it the trap handler for SIGTSTP runs. But if I run "(trap 'echo stop' SIGTSTP; trap; sleep 5)", it does not (after pressing ^z, ^\ is the only wait to quit since ^z and ^c do not work and sleep never completes). This behavior is explainted here: stackoverflow.com/questions/12824637/… – Curtis Yallop Jan 11 '18 at 16:10
2

With bash on Ubuntu 15.04, it seems to work as expected... how do you write your script?

#!/bin/bash
set -e
trap 'echo TSTP' TSTP
trap 'echo TTIN' TTIN
trap 'echo TTOU' TTOU
trap
while :; do read a; done # wait forever

Sample session (typed 3 times Ctrl+Z then Ctrl+C):

$ ./signal.sh
trap -- 'echo TSTP' SIGTSTP
trap -- 'echo TTIN' SIGTTIN
trap -- 'echo TTOU' SIGTTOU
^ZTSTP
^ZTSTP
^ZTSTP
^C

I found in the manual that bash ignores those signals when it's interactive and job control is enabled:

  1. Job Control (see Job Control) is enabled by default. When job control is in effect, Bash ignores the keyboard-generated job control signals SIGTTIN, SIGTTOU, and SIGTSTP.

You can disable job cotnrol by set +m. However, bash won't allow to update handlers in interactive mode anyway:

$ bash -c trap
$ bash -c 'trap true TSTP && trap'
trap -- 'true' SIGTSTP
$ bash -i -c trap
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
$ bash -i -c 'trap true TSTP && trap'
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
$ bash -i -c 'set +m && trap true TSTP && trap'
trap -- '' SIGTSTP
trap -- '' SIGTTIN
trap -- '' SIGTTOU
  • I'm trying to use it in a similar way to your script, just without set -e and just like command, not a script - simply by typing trap -- 'echo TSTP' SIGTSTP to the terminal. – Eenoku Sep 7 '15 at 11:11
  • Updated. I'm still not sure about trap builtin behavior in interactive shell. Might as well look into sources. – yaegashi Sep 7 '15 at 13:23
0

You did not mention which shell you are using, so in case this is a bug in the shell, there is no way to help you.

Looking at the Bourne Shell man page: http://schillix.sourceforge.net/man/man1/bosh.1.html you see the following text:

     bolic  names. Any attempt to set a trap on a signal that
     was ignored on entry to the current  shell  is  ineffec-
     tive.  An  attempt  to  trap on signal 11 (memory fault)
     produces an error.code here

So everything may be OK but your shell was called with these signals ignored.

If you are on a modern OS, you may call

 `psig $$`

to get the signal disposition of your current shell and you may replace $$ by other process IDs to check other processes.

  • I'm using Bash (version now added in the end of the description). And, on Lubuntu I'm getting the messsage "psig command not found". – Eenoku Sep 7 '15 at 11:10
  • psig is not a shell builtin, but a system program based on procfs-2 features. It was introduced by Solaris in 1994. It should be available on modern OS distributions. – schily Sep 7 '15 at 11:22

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.