I want to fire a command like ” rm -rf /etc/XXX.pid” when the shell script is interrupted in the middle of its execution. Like using CTRL+C
Can anyone help me what to do here?
I want to fire a command like rm -rf /etc/XXX.pid when the shell script
Share
Although it may come as a shock to many, you can use the
bashbuilt-intrapto trap signals 🙂Well, at least those that can be trapped, but
CTRL-Cis usually tied to theINTsignal (it can be changed withsttybut let’s discount that possibility for simplicity).You can therefore trap the signals and execute arbitrary code. For example, the following script will ask you to enter some text then echo it back to you. If perchance, you generate an
INTsignal, it simply growls at you and exits:A test run transcript follows (a fully entered line, a line with pressing
CTRL-Cbefore any entry, and a line with partial entry before pressingCTRL-C):