Using Traps in Bash Scripts
Gabriel M.
Linux Systems Engineer | IT Infrastructure | Security | Virtualization | Automation | AI | C and Shell Scripting
Imagine that you have a script to perform some critical operation, one that would render the system completely unusable if interrupted, as well as loosing and corrupting data. I am sure you just remembered about some tragic sad stories from the past, didn't you? Well, I do remember some terrible situations in which I went to help some friends recovering systems shot in the head by the execution of "innocent scripts".
Many of those situations (not to say all of them) could be avoided by using traps inside those "innocent scripts". What is a trap?
In short, a trap is a Bash builtin command, defined in trap.c, that capture signals and then execute the provided arg. It has the syntax:
trap arg signal [signal ...]
So, for example, when you hit ^C, ^D, ^Z, ..., you are sending a signal to a running job. In the same way, when you issue a kill command, you are sending a signal to a program (using its PID). Traps catch these signals and execute the action provided as arg.
Let's see that in action. First, imagine the reckless (snippet) version of an "innocent script".
#!/bin/bash do_critical_stuff() { MEANING_OF_LIFE=42 } [...] # Prepare to do critical operation ... do_critital_stuff # keep going with the rest of the script... [...]
Now, imagine what would happen if your script crashed/got killed exactly during the do_critical_stuff() function execution! Yes! You have lost the meaning of life! How tragic that would be! ;) Serious, now ... imagine that instead of this fun snipped, that function would perform some server critical operation! You should have some way of preventing this script from being put down and taking the whole system with it. Now enter the trap command! Check the same script, but with trap implemented.
#!/bin/bash do_critical_stuff() { MEANING_OF_LIFE=42 } trapped() { echo -e "You can't stop now!" } [...] # create the trap trap trapped INT TERM KILL Prepare to do critical operation do_critital_stuff # get rid of the trap (or it will be executed when this script exits) trap - TERM INT KILL # keep going with the rest of the script...
[...]
Did you see that? You made little modification to the script, but now that "innocent script" is aware of the dangers that might arise from its responsibilities. You took care of anyone trying to kill your script, as well as sending interrupts to it.
Keep that in mind when writing your next critical section within any of your scripts.
Happy scripting!
--FIN--
Backend Software Developer
4 å¹´Great to learn more about Linux! People should be more involved with it, since so many different companies use it, and not many of us had the opportunity to learn more about it. Thank you for sharing your knowledge with the community Gabriel!