Skip to content

Instantly share code, notes, and snippets.

@iandexter
Created May 28, 2010 07:38
Show Gist options
  • Save iandexter/416884 to your computer and use it in GitHub Desktop.
Save iandexter/416884 to your computer and use it in GitHub Desktop.
Log output in syslog format
LOG_FILE="/var/log/$(basename $0 .sh).log"
logMessage() {
COMMAND="$*"
sh -c "${COMMAND} | awk '{
\"echo \\\"$(date +%b\ %e\ %T) $(hostname) $(basename $0)[$$]:\\\"\"|getline timestamp;
close(\"echo \\\"$(date +%b\ %e\ %T) $(hostname) $(basename $0)[$$]:\\\"\");
printf(\"%s %s\n\", timestamp, \$0)
}'"
}
[ ! -e $LOG_FILE ] && touch $LOG_FILE && logMessage echo "Starting $(basename $0)..." > $LOG_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment