Shell Script get CTRL+Z with Trap

后端 未结 2 536
感动是毒
感动是毒 2021-01-03 05:40

I am trying to get the SIGSTOP CTRL+Z signal in my script\'s trap.

When my script is executing, if I temporarily suspend

相关标签:
2条回答
  • 2021-01-03 06:26

    There are two signals you can't trap*, SIGKILL and SIGSTOP. Use another signal.

    *: without modifying the kernel

    IEEE standard:

    Setting a trap for SIGKILL or SIGSTOP produces undefined results.

    0 讨论(0)
  • 2021-01-03 06:38

    The problem here is you are trying to suspend a process that is already sleeping.

    It is also good practice to use DIR=$(mktemp -d) in shell scripts to create temp directories.

    CTRL-C is signal (2) / CTRL-Z (20):

    catch_exits() {
            printf "\n$(basename $0): exiting\n" 1>&2
            rm -rf $DIR
            exit 1
    }
    
    trap catch_exits 1 2 3 15 20
    
    DIR="$(mktemp -d)"
    read -p "not sleeping" test
    

    if you send a function to the background (such as for a cursor spinner) - then you need to disable CTRL-Z while the long process is running with:

    trap "" SIGTSTP
    
    0 讨论(0)
提交回复
热议问题