When I run my script it runs okay, but there are major delays in the output. Each child runs a remote ssh command on the IPs listed in %hosts. The command can optionally take grep arguments. It seem like the last child gets priority in printing to STDOUT. What am I missing? Why don't all three children print nearly evenly and why the delays? The delays are as follows, I can open another window and watch directly on the remote server using tail -f, as the log file grows. But the output of my script is always 3-4 log entries behind. The log gets updated about every 10 seconds, so 40 seconds is a long time.
Thanks,
Aaron
#!/usr/bin/perl
$|=1;
%hosts=(
"host1" => "192.168.168.32",
"host2" => "192.168.168.33",
"host3" => "192.168.168.34"
);
$file = "/var/log/bluebolt/bluebolt.log";
$tail = "/usr/bin/tail";
$ssh = "/usr/bin/ssh";
$grep = "/bin/grep --color=always";
#die "Usage: $0 <file>\n" unless @ARGV;
if ($ARGV[0] eq "--help"){
print "Usage $0 [pattern]\n";
print "$0 will show all log files for App servers\n";
print "$0 [pattern] will grep the log files for the pattern\n"
+;
exit;
}
$SIG{CHLD} = 'IGNORE';
$SIG{INT} = sub {done("Received SIGINT")};
sub done {
print "@_\n";
exit;
}
foreach $key (keys %hosts) {
if(fork){
#parent process does nothing
} else {
#child process does all the work
#add any grep commands here
if ($ARGV[0]) {
if ($ARGV[1]) {
$command = "$ssh $hosts{$key} $tail -
+f $file | $grep $ARGV[0] $ARGV[1]";
}else{
$command = "$ssh $hosts{$key} $tail -
+f $file | $grep $ARGV[0]";
}
} else {
$command = "$ssh $hosts{$key} $tail -f $file";
}
open (README, "$command |") or die "Couldn't fork: $!\
+n";
select README;
$|=1;
while(<README>){
print STDOUT "$hosts{$key}: ";
print STDOUT $_;
}
}
}
while(1){sleep(5);}#parent process needs to wait for Ctrl-C
exit;
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.