Search code examples
perltimeiopiping

How to timestamp application output with perl


I have a command line app that gives command line output as it runs. When I write it to a file (ex: app >> log.txt) it all shows up in the text file just like it appeared in terminal. I need to be able to timestamp the output so that if I leave it running I can come back and see when it printed.

I have tried piping to a perl script:

#!/usr/bin/env perl
use strict;
use warnings;
while (<STDIN>) {
  print time.": $. : $_";
}

Using this command:

./app | perl timestamp.pl >> test.log 2>&1

But the log is empty. I'm sorry I can't be more specific about the application I'm running.


Solution

  • Suffering from buffering? Set

    $| = 1;
    

    at the top of your script.