4

To tail a file over ssh, I can use

ssh  -t remotebox "tail -f /var/log/remote.log"

However, it is possible to sync the tail with a local file? So a file automatically mirror the update of the tail command?

9

Yes, you can usee tee for that:

ssh  -t remotebox "tail -f /var/log/remote.log" | tee -a /var/log/local.log

This way output will be printed on both stdout and copied over into /var/log/local.log on the system you are running ssh command from.

1
  • is it possible to do the opposite - to tail local file to the remote server over ssh? – SET Jun 3 '19 at 21:48
1

Just redirect the stdout.

ssh -t remotebox tail -f /var/log/remote.log > local.log

To append to local.log:

ssh -t remotebox tail -f /var/log/remote.log >> local.log

This will write to the local server.

The remote server would only be written to if you include the redirect in the quotes:

ssh -t removebox "tail -f /var/log/remote.log >> remote.log"

0

Just suggest another useful way is using logtail. Install logtail package to the remote host, then run this to get only new lines that have not been read last time:

ssh remotebox logtail -f /var/log/remote.log >> local.log

You can run above command whenever you want to "sync" log, or you can add it to crontab, so it could run automatically. This way is especially userful when you dont have a stable connection, so you can not keep the ssh connection day by day.

Hope this help.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.