New answers tagged


The exit status of a killed command should be the signal number plus 128. So you can use the exit status to find out which signal killed you process. I tested it like this on Linux in the shell: print_exit_status_for_signal () { ( sleep 1000 echo Exit staus $? = signal $(( $? - 128 )) ) & sleep 1 killall "${1:+-$1}" sleep } ...


The man page for bash states If bash is waiting for a command to complete and receives a signal for which a trap has been set, the trap will not be executed until the command completes. In your example, the trap won't execute until the sleep 100 has completed.


Actually it did print, but you have to wait 100 seconds to see the result. You said: $./ > output & [4] 42624 If you check ps aux, you will get something similar to: xiaobai 42624 0.0 0.1 118788 5492 pts/3 S 04:07 0:00 /bin/bash xiaobai 42626 0.0 0.0 108192 668 pts/3 S 04:07 0:00 sleep 100 Your main script ...


trap works with signal numbers (i.e. trap clean_up 1) or with names without "sig" prefix (i.e. trap clean_up HUP). Furthermore, SIGSTOP cannot be trapped.


Pressing Ctrl+C in a terminal sends the SIGINT signal to the process running in the terminal. (More precisely, to all processes in the foreground process group; for example, if you're running foo | tail -f then the signal is sent to both foo and tail.) The conventional meaning of SIGINT (INTerrupt signal) is “abort the current task and return to an ...


The given trap command told the shell to intercept these signals: 1 SIGHUP 2 SIGINT 3 SIGQUIT 15 SIGTERM The "" is the command to be run if one of those signals is received. In other words, do nothing; ignore the signals completely. You can see what characters are bound to signals using stty -a, e.g., on the second line of output from stty in this ...


trap is a shell-builtin, the normal format is trap cmd list_of_signals. cmd is empty in your case, i.e. ignore that signal. 1 is the hangup signal, 3 is intr or Ctrl-C. See man bash Section Shell Builtin Commands for further explanation.

Top 50 recent answers are included