Steven
Steven

Reputation: 11

Why trap does not work to interrupt nohup command?

this is a script, the name is test.sh

#!/bin/bash
cleanup(){
    echo "Caught SIGINT ..."
    exit 1

}    
trap 'cleanup' 3
    while :;do
        echo "a"
        sleep 3
    done

if I run nohup ./test.sh & in linux bash directly , it can catch kill -3 PID signal ,

but if I write nohup ./test.sh & in a script file that name is go.sh) ,and I run go.sh , an then test.sh can't catch kill -3 PID signal

how can I write the go.sh script to make sure test.sh catch -3 signal ?

Upvotes: 1

Views: 778

Answers (1)

pynexj
pynexj

Reputation: 20688

kill -3 (SIGQUIT) would not work. According to bash's manual:

When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive shell), and SIGINT is caught and handled (so that the wait builtin is interruptible). In all cases, bash ignores SIGQUIT. If job control is in effect, bash ignores SIGTTIN, SIGTTOU, and SIGTSTP.

Upvotes: 1

Related Questions