53

Is there a way to remote tail 2 files?

I have two servers(a and b) behind a load balancer and I would like to do something like this if possible:

tail -f admin@serverA:~/mylogs/log admin@serverB:~/mylogs/log

Thanks!

4
  • Why did you erase the answer? Feb 13, 2010 at 0:20
  • mostly because it did not work :-)
    – mat
    Feb 13, 2010 at 0:20
  • @mat Oh I was just about to try it :). Thanks anyways! Feb 13, 2010 at 0:25
  • it does not work because the <(foo) construct will wait for the inner command to finish before being replaced by a pipe in the original command line from which the software will be able to read, and, tail would never finish :-)
    – mat
    Feb 13, 2010 at 0:28

12 Answers 12

32

This worked for me:

ssh -n user@hostname1 'tail -f /mylogs/log' &
ssh -n user@hostname2 'tail -f /mylogs/log' &
5
  • Yep, that's how i do it, except i usually have multiple gnome-terminals open, each with one session inside it Feb 13, 2010 at 8:27
  • 1
    Is there a way to do this without opening multiple independent ssh sessions?
    – tgies
    Mar 20, 2013 at 1:09
  • 9
    But this does not allow you to stop the tailing with Ctrl+C.
    – sorin
    Apr 28, 2014 at 16:40
  • @sorin to terminate the processes, just bring each one back to the foreground with fg; then you can ctrl+c it. You'd do that once for each tail you have running. Or you could pkill -f ssh to target all of them of them at the same time. Nov 22, 2017 at 18:33
  • To kill: kill $(jobs -p). Or to make ctrl-c kill: trap 'kill $(jobs -p);trap - INT' INT Oct 6, 2021 at 16:39
40

My preferred option is to go with multitail. I'd run something like:

multitail -l 'ssh user@host1 "tail -f /some/log/file"' -l 'ssh user@host2 "tail -f /some/log/file"'
4
  • 1
    Thanks! I was looking for an example of how to use multitail over ssh / on remote hosts! Feb 25, 2015 at 16:07
  • Multitail is a far more robust solution. Apr 6, 2015 at 18:14
  • our website has more than 10 web nodes, and multitail fails in the first try with the error Operation not permitted, with 2 3 web nodes, it's ok
    – Hieu Vo
    May 28, 2015 at 11:29
  • I also get the Operation not permitted, any clues?
    – user22711
    Oct 30, 2015 at 11:05
9

You can use fabric to tail several hosts (and also grep results, if needed):

$ fab -P -u 'USER' -p 'PASSWORD' --linewise -H host1,host2,host3 -- tail -f /path/to/my/log.log | grep ERROR
7

I was thinking it might also be possible to use:

ssh -f user@hostname1 "tail -f /var/log/file" > /tmp/somefile &
ssh -f user@hostname2 "tail -f /var/log/file" > /tmp/somefile &

The -f option after ssh allows you to enter a password before it runs in the background. Then you could have the line-by-line results in a single file and running:

tail -f /tmp/somefile

Would give you a little more control over the current "tail" command in case you wanted to use other tail options for displaying output.

6

Check out this answer on stackoverflow -- it uses dsh and tail -f.

5

Take a look at multitail. Just like the examples above, you can give it as a command to ssh, and then you will end up with one screen displaying (and buffering for easy scrollbacks) multiple logs. It also does coloring, which is very useful for spotting anomalies.

2

Parallel ssh has a nice solution for this:

pssh -t0 -H 'host-01 host-02 host-03 host-04' -P tail -f /var/log/app.log
  • -t0 disables the connection timeout, otherwise pssh closed the connection
  • -H '<host>...' is the list of hosts to run the command
  • -P is to enable printing the stdout of each host
  • tail -f /var/log/app.log can be any command that your remote shell can run
1

What would you say about something like this? http://gist.github.com/303175

1

Just a weird solution, but it works!:

Screen 1

ssh user@hostname1 "tail -f /var/log/file" > /dev/pts/6

Screen 2

ssh user@hostname2 "tail -f /var/log/file" > /dev/pts/6
1
  • this will work, outputting lines on the /dev/pts/6 terminal (to find out your own terminal : execute: who am i and it will show it to you.) May 10, 2019 at 16:56
1

You can checkout dbitail.

A Java tool I created, able to read local and distant log files using SSH. It is fairly simple to use.

Some more explanations: https://github.com/pschweitz/DBITail/wiki

Just download version corresponding to your operating system, of native jar release executable within your Java Runtime (requires java 8_40 or higher):

https://github.com/pschweitz/DBITail/releases

You can find a complete documentation (embedded with and I Github's page as well)

Philippe

0

or you can use screen to create two terminals and then split them.

SSH into the first host you want to tail the log file of and before running tail, enter screen.

Then tail the file on this host.

CTRL+A, c will create a new screen or window. From this new window ssh into the 2nd host and tail the file there.

Now to create the split screen

CTRL+A, SHIFT+S, this will create a split screen with current shell in the top part and the bottom part empty. To switch to the bottom part and put the first screen within it,

CTRL+A, TAB, this will move you to the bottom part. Now to bring your first SSH session into this area:

CTRL+A, " (quote symbol) and select the first session and hit enter.

I realize this may look like a lot of gymnastics, and it is, but you do get use to it once you use it a few times.

Here is a really good tutorial or quick reference to screen's commands: http://www.pixelbeat.org/lkdb/screen.html

Additionally, you can create a .screenrc file so that much of what I described can be automated the first time you enter screen.

Another nice thing about this is that you can just close your session and resume it some place else. For example, you're at work and you have some monitoring going on. You can just close your session without logging out, and resume when you get home or to another location.

0

Expanding on @einstiien's accepted answer, here is a bash script that cleans up the background processes on a Ctrl-C. It can be configured with a user and a list of servers. It assumes that you run the script on one of the servers in the list.

#!/bin/bash

# Purpose: Continuously tail and combine log files on current and other servers

# configuration:
#   servers, need to be hostname:
servers=("jimmy-neutron-01" "jimmy-neutron-02" "jimmy-neutron-03")
#   remote user:
user="jneutron"

function ctrl_c_cleanup() {
    echo
    echo 'Ctrl-C trap: kill remote tail processes:'
    plist=`ps -eaf | egrep "ssh .* $user[@].* tail \-f" | awk '{print $2}' | xargs echo`
    echo "kill -9 $plist"
    echo $plist | xargs kill -9
}

if [ $# -lt 1 ]; then
    echo "Purpose: Continuously tail and combine log files on current and other servers"
    echo "Example: $ xtail /var/log/nginx/access.log"
else
    echo "tail -f $1 across servers: ${servers[@]}"
    trap ctrl_c_cleanup INT  # trap Ctrl-C and call ctrl_c_cleanup()
    host=`uname --nodename`
    for server in ${servers[@]}; do
        if [[ $server != $host ]]; then
            ssh -t "$user@$server" "tail -f $1" &
        fi
    done
    tail -f "$1" # tail local log and wait for Ctrl-C
fi

#EOF

Save this as xtail, make it executable, and accessible via path.

Example usage:

$ xtail /var/log/nginx/access.log
tail -f /var/log/nginx/access.log across servers: jimmy-neutron-01 jimmy-neutron-02 jimmy-neutron-03
...continuous log output...
^C
Ctrl-C trap: kill remote tail processes:
kill -9 116919 116921

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .