Adding timestamps to each line of output
Have a long running script or command? Would you like to know where it’s spending its time?
The tool to use is ts which is distributed as part of moreutils.
If you’re using a Red Hat product (Fedora / Red Hat Enterprise Linux, CentOS, etc) you can get the ts command by installing the moreutils rpm via yum.
yum install moreutils
Usage is simple. Pipe the output from your script/command to ts and, optionally, supply a format spec. The example below is the hour of the day (%H – 24 clock), the minute (%M), and the second with microsecond fraction (%.S). I added the pipe (|) just to make the output easier to read.
$ ping google.com | ts '%H:%M:%.S |'
18:51:47.874676 | PING google.com (74.125.227.96) 56(84) bytes of data.
18:51:47.874889 | 64 bytes from dfw06s16-in-f0.1e100.net (74.125.227.96): icmp_req=1 ttl=55 time=18.0 ms
18:51:48.860208 | 64 bytes from dfw06s16-in-f0.1e100.net (74.125.227.96): icmp_req=2 ttl=55 time=16.3 ms
18:51:49.862409 | 64 bytes from dfw06s16-in-f0.1e100.net (74.125.227.96): icmp_req=3 ttl=55 time=17.0 ms
