Tuesday, May 8, 2012

How can I tail a remote file?

I am trying to find a good way to tail a file on a remote host. This is on an internal network of Linux machines. The requirements are:




  1. Must be well behaved (no extra process laying around, or continuing output)


  2. Cannot require someone's pet Perl module.


  3. Can be invoked through Perl.


  4. If possible, doesn't require a custom built script or utility on the remote machine (regular linux utilities are fine)




The solutions I have tried are generally of this sort



ssh remotemachine -f <some command>


"some command" has been:



tail -f logfile


Basic tail doesn't work because the remote process continues to write output to the terminal after the local ssh process dies.



$socket = IO:Socket::INET->new(...);
$pid = fork();
if(!$pid)
{
exec("ssh $host -f '<script which connects to socket and writes>'");
exit;
}

$client = $socket->accept;
while(<$client>)
{
print $_;
}


This works better because there is no output to the screen after the local process exits but the remote process doesn't figure out that its socket is down and it lives on indefinitely.





No comments:

Post a Comment