Last active
September 16, 2022 22:10
-
-
Save emmanuelnk/6b7835b241007633b4031da00c1e3ea4 to your computer and use it in GitHub Desktop.
Log bash commands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function log_cmd() | |
{ | |
"$@" | |
ret=$? | |
if [[ $ret -eq 0 ]] | |
then | |
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ") user[ $USER ] status[ Success ] code[ $ret ] cmd[ $@ ]" >> bash_command.log | |
else | |
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ") user[ $USER ] status[ Error ] code[ $ret ] cmd[ $@ ]" >> bash_command.log | |
exit $ret | |
fi | |
} | |
# example | |
log_cmd ls | |
# bash_command.log | |
# 2022-09-16T22:04:46Z user[ emmanuel ] status[ Success ] code[ 0 ] cmd[ ls ] | |
log_cmd lklj | |
# bash_command.log | |
# 2022-09-16T22:09:06Z user[ emmanuel ] status[ Error ] code[ 127 ] cmd[ lklj ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment