Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently I have my bash script using exec 22>&2 21>&1 1>$log 2>&1 to write stderr and stdout to a log file. I would like to add a time stamp for each entry, but I haven't found an easy way to do that. Ideally it would be a simple change to the current command with the time being written on the same line before the rest.

Here's a script that I'm using the command in:


#This script takes the server to rysnc as an argument. You can also tell 
#the script to check the server_status.txt file.
#Example: /path/to/script/ grail true
#The arguments are order senstive. The server name must come before the status
#check value.


DIRECTORYS="auth/ keys/ log/mailwhen/ intranet/ www/calmaa/data/ www/admatch/data/ www/sfhsa/data/ www/hfa3_org www/padmatch/ www/serverdown/"

if [ "x$2" == "xfalse" ]; then
    return 0
elif [ "x$2" == "xtrue" ]; then
    if [ `cat /srv/www/wan*/server_status.txt` == "primary" ]; then
        exit 0
    echo "Please use \"true\" or \"false\" for the second value." 
    exit 1

# Copy stdout and stderr, and then open the logfile
exec 22>&2 21>&1 1>$log 2>&1
# Here is how to restore stdout and stderr:
# exec 2>&22 1>&21

    rsync -azu --delete --bwlimit=500 $1:/srv/$DIRECTORY  /srv/$DIRECTORY
share|improve this question
up vote 2 down vote accepted

Without seeing more of your script I can't tell you the best way for your particular needs. However this is a general way that can be adapted for your needs.

exec > >(while read -r line; do printf '%s %s\n' "$(date --rfc-3339=seconds)" "$line"; done)

Each line of text that is output will have the timestamp for the time that it occurred prepended. The output will look something like this:

2013-09-04 21:32:14-05:00 An event occurred and this is the message
2013-09-04 21:32:37-05:00 Some time passed, another event produced a message
share|improve this answer
This is more in line with what I want. – Ironlenny Sep 5 '13 at 19:53
That works great except it doesn't handle stderr. That is still logged with out a time stamp. – Ironlenny Sep 5 '13 at 23:29
@Ironlenny: You should be able to add 2>&1 after the closing parenthesis. Without seeing how you're using the other file descriptors, I can't really tell you how to use them. You can try a separate exec statement for them before the main one. By the way, to redirect the timestamped line to your log file, you should be able to add >"$log" between the done and the closing parenthesis. – Dennis Williamson Sep 6 '13 at 0:44

edit stream with sed:

sed "s/^/$(date -u) /"

using pipe:

[root@giomacdesk ~]# cat test.txt 
[root@giomacdesk ~]# cat test.txt |sed "s/^/$(date -u) /"
ოთხ სექ  4 19:00:53 UTC 2013 asd1
ოთხ სექ  4 19:00:53 UTC 2013 asd2
[root@giomacdesk ~]# 
share|improve this answer
That gives the same date/time on every line. – Dennis Williamson Sep 5 '13 at 2:20
GioMac, your solution requires a file that is already written. I want the time stamp appended to the string that is being written to the log file. – Ironlenny Sep 5 '13 at 23:16

Your Answer


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.