Using Traps in Bash Scripts

Using Traps in Bash Scripts

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--




Thiago Andrade

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!

要查看或添加评论,请登录

Gabriel M.的更多文章

  • Docker 101

    Docker 101

    A (tiny) introduction to Docker. If you want to get your hands dirty with some Docker content, this quick introduction…

  • Building custom kernels for Linux + updating the system firmware.

    Building custom kernels for Linux + updating the system firmware.

    In this post I'll show you how you can replace your current Linux kernel with a new one built entirely from the…

  • Debian complained about missing firmware during installation? Add missing 'non-free' firmware to installation image! :)

    Debian complained about missing firmware during installation? Add missing 'non-free' firmware to installation image! :)

    If you have installed Debian, you might have faced the screen below: And probably got very frustrated, because your…

  • Safer Bash Scripting Tips

    Safer Bash Scripting Tips

    I hope you are all well and safe. As "stay safe" is the most used expression during this COVID-19 period, I thought it…

    1 条评论
  • Pointers and 2D-arrays in C

    Pointers and 2D-arrays in C

    When taking the Engineering or CS undergrad path, like 1+1 leads to 2 (let's keep it simple here, shall we?), students…

    2 条评论
  • Linux Kernel Inotify Subsystem

    Linux Kernel Inotify Subsystem

    When dealing with Unix-style filesystems there must be a data structure that is capable of describing an object from…

  • Linux File I/O

    Linux File I/O

    When using system calls for dealing with file I/O, open(), read(), write() and close() are the four functions used to…

  • Shifting bits in C

    Shifting bits in C

    Have you ever asked yourself how much memory space do you waste when writing your code? Sometimes you just need a…

  • Improving your Linux box security

    Improving your Linux box security

    Did you know that more than never, during these quarantine days, there is a lot more malicious activities undergoing…

  • Build UnrealEngine on Linux, making it use less disk space! :)

    Build UnrealEngine on Linux, making it use less disk space! :)

    If you are playing with the amazing Unreal Engine on your Linux box, you might have noticed that the final size after…

社区洞察

其他会员也浏览了