From 1131add1067394bd85d651784cbfb1d6d48dda2d Mon Sep 17 00:00:00 2001 From: Ryan Barth Date: Sun, 16 Apr 2017 01:02:21 -0700 Subject: [PATCH] add exit trap trick (#45) --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 0ffa121..6eb29b3 100644 --- a/README.md +++ b/README.md @@ -1095,6 +1095,18 @@ source ~/.bashrc cd $hotellogs ``` +## Exit traps + +Make your bash scripts more robust by reliably performing cleanup. + +```bash +function finish { + # your cleanup here. e.g. kill any forked processes + jobs -p | xargs kill +} +trap finish EXIT +``` + # 4. Debugging You can easily debug the bash script by passing different options to `bash` command. For example `-n` will not run commands and check for syntax errors only. `-v` echo commands before running them. `-x` echo commands after command-line processing.