multiple bash traps for the same signal

后端 未结 12 803
悲哀的现实
悲哀的现实 2020-12-13 01:47

When I use the trap command in bash, the previous trap for the given signal is replaced.

Is there a way of making more than one trap<

12条回答
  •  孤城傲影
    2020-12-13 02:49

    I would like to propose my solution of multiple trap functions for simple scripts

    # Executes cleanup functions on exit
    function on_exit {
        for FUNCTION in $(declare -F); do
            if [[ ${FUNCTION} == *"_on_exit" ]]; then
                >&2 echo ${FUNCTION}
                eval ${FUNCTION}
            fi
        done
    }
    trap on_exit EXIT
    
    function remove_fifo_on_exit {
        >&2 echo Removing FIFO...
    }
    
    function stop_daemon_on_exit {
        >&2 echo Stopping daemon...
    }
    

提交回复
热议问题