Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a constantly running script that I output to a log file:

script.sh >> /var/log/logfile

I'd like to add a timestamp before each line that is appended to the log. Like:

Sat Sep 10 21:33:06 UTC 2011 The server has booted up. Hmmph.

Is there any jujitsu I can use?

share|improve this question
2  
See this questin. serverfault.com/questions/80749/…. A couple answer would apply here. –  Zoredache Sep 11 '11 at 0:43
    
For an awk/gawk solution see: stackoverflow.com/questions/21564/… –  User Nov 2 '12 at 17:31

6 Answers 6

up vote 22 down vote accepted

You can pipe the script's output through a loop that prefixes the current date and time:

./script.sh | while IFS= read -r line; do echo "$(date) $line"; done >>/var/log/logfile

If you'll be using this a lot, it's easy to make a bash function to handle the loop:

adddate() {
    while IFS= read -r line; do
        echo "$(date) $line"
    done
}

./thisscript.sh | adddate >>/var/log/logfile
./thatscript.sh | adddate >>/var/log/logfile
./theotherscript.sh | adddate >>/var/log/logfile
share|improve this answer
    
+1 this is a nice solution –  Iain Sep 10 '11 at 22:17
    
What is "IFS= " for? –  Nils Sep 15 '11 at 21:15
2  
@Nils it's a trick to prevent read from trimming whitespace at the beginning and and of the line. It sets IFS (bash's Internal Field Separator, basically a list of whitespace characters) to empty for the read command. –  Gordon Davisson Sep 15 '11 at 21:43
1  
...and -r ignores the escape-character "\". This should really work in all cases - great pice of scripting. –  Nils Sep 16 '11 at 20:47
2  
@Nils it's not completely bulletproof, since some implementations of echo interpret escape sequences. If you really want it not to mess with the content (other than adding dates), replace the echo command with printf "%s %s\n" "$(date)" "$line" –  Gordon Davisson Sep 17 '11 at 2:31

You can simply echo the command outputs to the logfile. ie,

echo "`date -u` `./script.sh`" >> /var/log/logfile

It really works :)

Example:

[sparx@E1]$ ./script.sh 
Hello Worldy
[sparx@E1]$ echo "`date -u` `./script.sh`" >> logfile.txt
[sparx@E1]$ cat logfile.txt 
Mon Sep 12 20:18:28 UTC 2011 Hello Worldy
[sparx@E1]$ 
share|improve this answer
    
Hmm isn't working for me. –  Antonius Bloch Sep 12 '11 at 19:11
    
What are you getting when you execute the command ? –  SparX Sep 12 '11 at 20:15

The date command will provide that information

date -u
Sat Sep 10 22:39:24 UTC 2011

so you can

echo $(date -u) "Some message or other"

is that what you wanted ?

share|improve this answer
    
Using the date command was kind of what I had in mind, but I can't really add that to the script itself, so what I'm looking for is a way to change this line: "script.sh >> /var/log/logfile" to append the date. –  Antonius Bloch Sep 10 '11 at 21:53
    
In that case redirect the output of your script to a named pipe and have a daemon listening for output which takes the script output and adds a date before writing it to a log file. You can probably modify the script I wrote here to do this. I would do it because it interests me but it's late in the UK and I have an early start tomorrow. –  Iain Sep 10 '11 at 22:12

See "ts" from the Ubuntu "moreutils" pkg:

command | ts

Or, if $command does automatic buffering (requires expect-dev pkg):

unbuffer command | ts
share|improve this answer

You mean like:

(date && script.sh) >> /var/log/logfile
share|improve this answer
    
My god, people, everyone is doing back tick replacement, named pipes, etc. Just enclose both the date command and the script in parens! The guy who has the function has a legit case if there's multi-line output and the log needs to look pretty with the date on every line, but most of these solutions are overkill that doesn't use shell semantics. –  cjc Sep 12 '11 at 21:11
1  
That will only add the timestamp once per execution of script.sh. The OP needs a timestamp per line. –  Dave Forgac Oct 10 '12 at 14:10
    
although this doesn't answer the OP question I still found it useful info. –  User Nov 2 '12 at 20:14

Make a config.sh file

#!/usr/bin/env bash
LOGFILE="/path/to/log.log"
TIMESTAMP=`date "+%Y-%m-%d %H:%M:%S"`

When you need to send to log file use

#!/usr/bin/env bash
source /path/to/config.sh

echo "$TIMESTAMP Say what you are doing" >> $LOGFILE

do_what_you_want >> $LOGFILE

Log file will looks like

2013-02-03 18:22:30 Say what you are doing

So it will be easy to sort by date

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.