A lot of time when executing a cronjob or a long running command I capture the standard out and standard out to a log file.This works okay but without time stamps it isn’t really useful especially for a job that runs many times a day which makes it difficult to tell which lines in the log match the run.What I do now is copy a script to all my systems (using chef of course) which will annotate any output I pipe to it.A command line example:
dodell@spork/etc$ cat resolv.conf | /usr/local/bin/annotate.sh Thu Sep 6 14:39:59 PDT 2012: # Automatically generated, do not edit Thu Sep 6 14:39:59 PDT 2012: nameserver 173.203.4.8 Thu Sep 6 14:39:59 PDT 2012: nameserver 173.203.4.9
Okay not a super useful example but you get my point.This is even more useful when added to a cronjob:
1 0 * * * /usr/local/bin/percona_backup_and_restore.sh backup 2>&1| /usr/local/bin/annotate.sh >> /var/log/mysql/xtrabackup.log
and the output:
Thu Sep 6 00:01:02 PDT 2012: Thu Sep 6 00:01:02 PDT 2012: InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy Thu Sep 6 00:01:02 PDT 2012: and Percona Inc 2009-2012. All Rights Reserved. Thu Sep 6 00:01:02 PDT 2012: Thu Sep 6 00:01:02 PDT 2012: This software is published under Thu Sep 6 00:01:02 PDT 2012: the GNU GENERAL PUBLIC LICENSE Version 2, June 1991. Thu Sep 6 00:01:02 PDT 2012: Thu Sep 6 00:01:02 PDT 2012: 120906 00:01:02 innobackupex: Starting mysql with options: --password=xxxxxxxx --user='debian-sys-maint' --unbuffered -- Thu Sep 6 00:01:02 PDT 2012: 120906 00:01:02 innobackupex: Connected to database with mysql child process (pid=19867) Thu Sep 6 00:01:08 PDT 2012: 120906 00:01:08 innobackupex: Connection to database server closed Thu Sep 6 00:01:08 PDT 2012: IMPORTANT: Please check that the backup run completes successfully. Thu Sep 6 00:01:08 PDT 2012: At the end of a successful backup run innobackupex Thu Sep 6 00:01:08 PDT 2012: prints "completed OK!".
Ah, how beautiful standard out and error with time stamps…….magic.
The code:
#!/bin/bash while read line do echo "$(date): ${line}" done