3

I am trying to monitor a remote, embedded host that is writing output to a file: /var/log/myapp.log

This host may lose power for hours. The app may be killed and restarted.

On my local machine, I want to capture the myapp.log contents in real-time as it gets updated.

The basic script I have does this:

ssh user@remote_host_ip 'tail -f /var/log/myapp.log' | tee -a ~/logs/myapp.log

This works for the simple case where the remote host is already up and can be SSH'd into. I would like something that will continuously try to SSH into the remote host over and over until it succeeds, and then run the tail -f ... command and capture the output locally. I want to avoid having to rerun this program if the remote host loses power.

From what I have searched so far, it is sounding like I may want to use some combination of autossh and screen.

I tried playing with the rscreen script included with autossh but have not had much luck. Here is the modified script, which takes another argument for the command to run on the remote host. I called the modified script rscreen_myapp:

#!/bin/sh
#
# MODIFIED (not working) sample script to use autossh to open up a remote screen
# session, or reconnect to an existing one.
#
# $Id: rscreen,v 1.4 2002/05/07 17:54:13 harding Exp $


autossh -M 20004 -t $1 "screen -e^Zz -D -R -X $2"

But when I run: ./rscreen_myapp remoteuser@remotehost "tail -f /var/log/myapp.log", I get:

Agent pid 28990
Identity added: /home/localuser/.ssh/id_rsa (/home/localuser/.ssh/id_rsa)
No screen session found.
Connection to 10.10.3.9 closed.

I am struggling with screen and admittedly confused by it... What am I doing wrong? Am I not using the -X argument properly? Or do I need to do something else altogether? Do I need to somehow make use of the screenlog.n files? (I would rather avoid the .n unique identifier and prefer just having myapp.log on the local machine.)

Ultimately, this script/program would run automatically and in the background on my local machine. So as long as the local machine is on, it will try to capture/mirror the log from the remote machine whenever possible and indefinitely.

  • Have you tried that without both the -e and the -X options? Ctrl-z is used for job-control on modern shells. – ott-- Jul 21 '15 at 19:38
  • You could log to a more reliable logging server. However this is not good as you new make the main server less reliable. Alternatively use synchronisation software to sync the logs to the other machine. Synchronisation software can cope with machines periodically going off-line. – ctrl-alt-delor Jul 21 '15 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.