Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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