4

How can I receive the output of two or more independent processes in a third location without affecting the two processes?

I have two processes, A and B, each running in their own screen, continuously outputting stuff.

I can run screen and attach to A to see its output:

12:00 Foo.
12:02 Foo.
12:04 Foo.

Same with B:

12:01 Bar.
12:03 Bar.
12:05 Bar.

And I can combine multiple screens to see both side-by-side or similar.

But I am looking for a way to see the output of these two processes combined into one "stream" of messages:

12:00 Foo.
12:01 Bar.
12:02 Foo.
12:03 Bar.
12:04 Foo.
12:05 Bar.

While also not being able to inadvertently send something like CTRL + C to one of the processes. (I still want to be able to reattach to the processes and interact with them from time to time, which is why I've been using screen.)

Thus I don't think I would want to run the two processes together and look at the output directly.

I could use strace to do something like this:

strace -PIDofA -e write &
strace -PIDofB -e write &

But the output is not very pretty:

write(1, "12:00 Foo.", 10) = 10
write(5, "Foo in file.", 12) = 12
write(1, "12:01 Bar.", 10) = 10
write(5, "Bar in file.", 12) = 12
...

and it doesn't feel like a good solution to run multiple strace this way to get the combined output.

Perhaps I could make both processes write to a file, and do something like:

tail -f output.txt

But I'm not sure if that will cause problems as the file gets filled with more and more lines of output.

And I'm not sure what happens when two processes attempt to write to the same file at the same time.

So what tool do I use, or how do I redesign my processes so as to display the output of A and B together?

(I'm running this on Debian and accessing it through ssh, if that makes a difference.)

  • This may not be of interest to anyone, but it's been a couple of months, and a hundred thousand lines of logging; I went with the option of doing command 2>&1 | tee -a logfile.log and so far it's chugging along quite happily with no apparent adverse effects from the moderately large amount of text. – Holistic IT Aug 17 '17 at 10:51
2

You can pipe the output of each command to tee file and tail -f the file. There is no synchronization between the processes so the output will be interleaved (in possibly ugly fashion). If you are worried about filling up the disk, you might be able to output to a named pipe instead:

[first screen]
$ mkfifo /tmp/foo
$ tail -f /tmp/foo

[second screen]
$ command1 | tee /tmp/foo

[third screen]
$ command2 | tee /tmp/foo
  • This appears to do exactly what I need, so I'll accept it. However there are some confounding factors outside the scope of the question that I will have to work around for my own use case, so in the end I might do something closer to derobert's answer. Thanks! – Holistic IT Jun 14 '17 at 16:40
3

Easiest way seems to be use screen's built-in logging capabilities. Screen-Command-Key [control-A], H, will have screen log output to screenlog.«window» (e.g., screenlog.0). If your two screens are in the same directory (and are the same window number), they'll wind up writing to the same log file—which appears to do what you want. If they're in different directories/different window numbers, you can then combine those two files, e.g., with tail -qf dir1/screenlog.0 dir2/screenlog.0.

Note that screen does buffer the output a little, so the mixing won't be perfect. You'll get a few lines from one of them, then a few from the other, etc.

You can set the file name and buffer time with the logfile and logfile flush options. Setting the flush time to 0 appears to work (Screen-command-key, :, then type logfile flush 0, then enter).

  • I see. In this case, I could simply make my processes write their relevant output directly to the file, or just pipe the output to the file, I guess. My concern is, does the output just pile up in the file? What do I do later when gigabytes of output have piled up in the file? Do I need to periodically log on (or set up a cron job or something) to empty out the file? – Holistic IT Jun 14 '17 at 15:45
  • @HolisticIT yes, you'll need to periodically clear out the file. If you can change what you're running, you could (for example) run it through cronolog to automatically rotate the log file. Or through syslog. Or systemd's journal. – derobert Jun 14 '17 at 15:48

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.