code_junkie

How do I implement 'tail -f' with timeout-on-read in Perl?

Nov 13th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. perl progress.tracker.pl progress.tracker.pl
  2.  
  3. #!/usr/perl/v5.10.0/bin/perl -w
  4. #
  5. # @(#)$Id: progress.tracker.pl,v 1.3 2009/01/09 17:32:45 jleffler Exp jleffler $
  6. #
  7. # Track progress of a log-generating process by printing one dot per line read.
  8.  
  9. use strict;
  10. use constant DOTS_PER_LINE => 50;
  11. use constant LINES_PER_BREAK => 25;
  12. use constant debug => 0;
  13. use POSIX qw( strftime );
  14. use Sys::AlarmCall;
  15.  
  16. sub read_line
  17. {
  18. print "-->> read_line()n" if debug;
  19. my $line = <STDIN>;
  20. printf "<<-- read_line(): %s", (defined $line) ? $line : "n" if debug;
  21. return $line;
  22. }
  23.  
  24. my $line_no = 0;
  25. my $timeout = 30;
  26. my $line;
  27.  
  28. $| = 1; # Unbuffered output
  29.  
  30. while ($line = alarm_call($timeout, 'read_line', undef))
  31. {
  32. $line_no++;
  33. print ".";
  34. print "n" if ($line_no % DOTS_PER_LINE == 0);
  35. printf "%sn", strftime("%Y-%m-%d %H:%M:%S", localtime(time))
  36. if ($line_no % (DOTS_PER_LINE * LINES_PER_BREAK) == 0);
  37. last if $line =~ m/^Trace run finished: /;
  38. }
  39.  
  40. print "n";
  41. print $line if defined $line && $line =~ m/^Trace run finished: /;
  42.  
  43. #!/usr/perl/v5.10.0/bin/perl -w
  44. #
  45. # @(#)$Id: progress.tracker.pl,v 3.2 2009/01/14 07:17:04 jleffler Exp $
  46. #
  47. # Track progress of a log-generating process by printing one dot per line read.
  48.  
  49. use strict;
  50. use POSIX qw( strftime );
  51. use File::Tail;
  52.  
  53. use constant DOTS_PER_LINE => 50;
  54. use constant LINES_PER_BREAK => 25;
  55. use constant MAX_TIMEOUTS => 10;
  56. use constant TIMEOUT_LENGTH => 30; # Seconds
  57.  
  58. my $timeout = TIMEOUT_LENGTH;
  59. my $line_no = 0;
  60. my $n_timeouts = 0;
  61. my $line;
  62.  
  63. sub print_item
  64. {
  65. my($item) = @_;
  66. $line_no++;
  67. print "$item";
  68. print "n" if ($line_no % DOTS_PER_LINE == 0);
  69. printf "%sn", strftime("%Y-%m-%d %H:%M:%S", localtime(time))
  70. if ($line_no % (DOTS_PER_LINE * LINES_PER_BREAK) == 0);
  71. }
  72.  
  73. $| = 1; # Unbuffered output
  74.  
  75. # The foreach and while loops are cribbed from File::Tail POD.
  76. my @files;
  77. foreach my $file (@ARGV)
  78. {
  79. push(@files, File::Tail->new(name=>"$file", tail => -1, interval => 2));
  80. }
  81.  
  82. while (1)
  83. {
  84. my ($nfound, $timeleft, @pending) = File::Tail::select(undef, undef, undef, $ti
  85.  
  86.  
  87. perl timeout
  88.  
  89.  
  90.  
  91.  
  92. link|improve this question
  93.  
  94.  
  95. edited Jan 18 '09 at 17:45
  96.  
  97. asked Jan 14 '09 at 0:19Jonathan Leffler112k787208
  98.  
  99. 100% accept rate
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. feedback
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. 2 Answers
  138.  
  139. active
  140. oldest
  141. votes
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. up vote
  158. 5
  159. down vote
  160. accepted
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. Have you tried File::Tail to handle the actual tailing instead of trying to coerce <STDIN> to do the job?
  168.  
  169. Or, if that piece does work, in what way is this failing?
  170.  
  171.  
  172.  
  173.  
  174. link|improve this answer
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. answered Jan 14 '09 at 0:23Dave Sherohman15.1k21850
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. No; I&#39;ve not yet tried it. But I will... I should have known, I suppose. My search on timeout didn&#39;t find that. &ndash;&nbsp;Jonathan Leffler Jan 14 '09 at 0:24
  198.  
  199.  
  200.  
  201.  
  202. How do you do it for a non-file device, like &#47;dev&#47;ttyUSB0
  203.  
  204. grep -m 1 pattern <(tail -n 0 -f file)
  205.  
  206. ssh login@hostname 'grep -m 1 pattern <(tail -n 0 -f file)'
  207.  
  208. ssh login@hostname 'grep -m 1 pattern <(unbuffer -p tail -n 0 -f file)'
Add Comment
Please, Sign In to add comment