1

I'm trying to tail two log files in different hosts and use the merged output as a single one.

ssh HOST_1 "tail -f MY_LOG_FILE"

I'm also using mkfifo to create a named pipe.

mkfifo MY_PIPE

The problem is. Outputting a local command to MY_PIPE works fine, but when I output my ssh command nothing can be read on the named pipe (tail -f MY_NAMED_PIPE in another terminal)

mkfifo MY_PIPE

ssh HOST_1 "tail -f MY_LOG_FILE" > MY_NAMED_PIPE

The commands above generate no output on the pipe.

Is there any parameter to ssh command to pipe it correctly?

Is there another way to pipe it?

1
  • 1
    You show MY_PIPE and MY_NAMED_PIPE. Is that intentional? Jan 17, 2015 at 1:20

3 Answers 3

1

You can also use tee with the append option (-a) to merge contents.

In terminal #1

ssh HOST_1 "tail -f /path/to/file" | tee -a /path/to/merged/contents

In terminal #2

ssh HOST_2 "tail -f /path/to/file" | tee -a /path/to/merged/contents

In terminal #3

tail -f /path/to/merged/contents
1
  • The problem is when lines are truncated by multiple lines written at the same time. – Rafael Borja just now edit Apr 21, 2015 at 1:57
1
ssh HOST_1 "tail -f MY_LOG_FILE" > MY_NAMED_PIPE

The tail program is going to buffer its output in blocks of 8 KB or so, because it's not writing to a TTY. If MY_LOG_FILE isn't very active, this may be why you're not seeing any output. Even if it were working, tail isn't writing full lines at a time, so the output from multiple tail instances would be badly jumbled.

You can tell ssh to run the tail command using a TTY. This should cause tail to buffer its output by lines instead. This should produce better results.

ssh -tt HOST_1 "tail -f MY_LOG_FILE" > MY_NAMED_PIPE

You can refer to the ssh man page here.

1
  • The problem is when lines are truncated by multiple lines written at the same time. Apr 21, 2015 at 1:56
0

I just did the following:

mkfifo MY_PIPE
ssh HOST_1 "tail -f /var/log/messages" > MY_PIPE

On the local machine I ran:

tail -f /var/log/messages > MY_PIPE

In another terminal on the local machine:

cat < MY_PIPE

The output of both remote and local tail -f /var/log/messages commands is visible.

The tutorial I followed is from Linux Journal.

1
  • The problem is when lines are truncated by multiple lines written at the same time. – Rafael Borja just now edit Apr 21, 2015 at 1:57

You must log in to answer this question.

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