Skip to content

Instantly share code, notes, and snippets.

@caiobegotti
Created October 8, 2014 22:01
Show Gist options
  • Save caiobegotti/e3c815acb2eef34a2d82 to your computer and use it in GitHub Desktop.
Save caiobegotti/e3c815acb2eef34a2d82 to your computer and use it in GitHub Desktop.
Log trapper, useful when you want to log all commands with a timestamp for further analysis (e.g. if your script echoes to a log file)
#!/bin/bash
function logtrap() {
local last="${BASH_COMMAND}"
local exitcode=${?}
# any format is ok but this will be printed BEFORE all commands, not after
echo "TRAPLOG: [$(date +%T)] [${exitcode}] [${last}]"
}
trap logtrap DEBUG
echo "testing"
false 12345
true 09876
ls -l /tmp | head
uname -a
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment