Adding timestamp for each line in nohup.out
After reading Bernd's nice tip, I now remember one of our colleague posting question to internal support engineer alias:
"How can I add timestamp to each line in nohup.out ?"
I didn't ask why the customer wanted to that. I thought customer was seeing intermittent output from JRE without any time&date info and they wanted to correspond those to real log files.
Anyway, now I know how to do it.
bash-3.00$ date
Fri Jan 11 00:38:52 JST 2008
bash-3.00$ nohup sh -c "iostat 5 2>&1 | nawk '{\"date \\\"+%m/%d/%Y %H:%M:%S\\\":\"|getline date;
close(\"date \\\"+%m/%d/%Y %H:%M:%S\\\":\");
printf (\"%s %s\n\", date, \$0)}'" &
[2] 30111
bash-3.00$ Sending output to nohup.out
bash-3.00$ tail -f nohup.out
01/11/2008 00:38:56: tty md0 md1 md2 md3 cpu
01/11/2008 00:38:56: tin tout kps tps serv kps tps serv kps tps serv kps tps serv us sy wt id
01/11/2008 00:38:56: 1 124 7 1 18 1 0 31 0 0 6 17 0 178 4 3 0 93
01/11/2008 00:39:01: 0 3 0 0 0 0 0 0 0 0 0 0 0 0 3 3 0 94
01/11/2008 00:39:06: 4 187 0 0 0 0 0 0 0 0 0 0 0 0 4 3 0 93
01/11/2008 00:39:11: 0 20 0 0 11 0 0 0 0 0 0 0 0 0 4 4 0 92
01/11/2008 00:39:16: 0 20 0 0 12 0 0 0 0 0 0 0 0 0 5 3 0 92
This may not work in non bash or for lot of other commands.
Cant comment on Bernd's blog (anymore), however using gawk is much more efficient, since it does not need to span a command:
gawk '{print strftime("%Y-%m-%d %H:%M:%S")}'
Greetings
Bernd
Posted by Bernd Eckenfels on January 10, 2009 at 09:33 AM JST #
Thanks, Bernd. I'll try if Solaris10 nawk can do it.
Posted by Katsumi INOUE on January 10, 2009 at 04:16 PM JST #