michael@slashetc:~$

Using Trap to Exit Bash Scripts Cleanly

In this tutorial we explain how to use the trap bash builtin to exit bash scripts cleanly. We explore examples of how to clean up temp files, ensure services stay running and close ports.

Article Source.