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.


Have you tried

ssh -t remotemachine <some command>

-t option from the ssh man page:

 -t      Force pseudo-tty allocation. This can be used to execute 
         arbitrary screen-based programs on a remote machine, which
         can be very useful, e.g. when implementing menu services.
         Multiple -t options force tty allocation, even if ssh has no local tty.

instead of

 -f      Requests ssh to go to background just before command execution.  
         This is useful if ssh is going to ask for passwords or passphrases, 
         but the user wants it in the background.
         This implies -n.  The recommended way to start X11 programs at a remote
         site is with something like ssh -f host xterm.

Some ideas:

  • You could mount it over NFS or CIFS, and then use File::Tail.
  • You could use one of Perl's SSH modules (there are a number of them), combined with tail -f.