-1

At work, I run many simulations from the command line, and I want to capture the output into a file, but also to continue to see the output in the command line. My current workflow is to run something like this:

run sim_0425 > sim_0425.log & tail -f sim_0425.log

This does exactly what I want, but the issue is that the logfile will be named something different each time depending on the simulation parameters (for example, here it's "sim_0425"), and it's kinda kludgey to manually rename it in all locations each time. I'll often forget to rename it one of the locations, and then I'll rewrite a file somewhere.

I can't imagine that there isn't some built-in way to output to a file and to the std out stream simultaneously. Googling this is challenging because I'm basically searching for "> & tail" and that doesn't turn much up :/

Drphoton
  • 164
  • 9
  • `the issue is that the logfile will be named something` No, from the code you posted, it will be named `sim_0425.log` each time. `output to a file` If it's not extreamely much data, output to syslog. `run ... | logger -p info.local7 & journalctl -f ..`. In that case, write a (user?) systemd service file, instead of manually managing background processes. – KamilCuk Apr 25 '22 at 16:04
  • The idea is that I'd rename the string "sim_0425" to something else depending on the changes I've made to my script, and I'd want the name of the log file to match it. And then when I call tail, I'd need to change it there as well. – Drphoton Apr 25 '22 at 18:53

1 Answers1

1

You can use the tee command for this (if you have it installed, which is likely; it's part of the Posix standard). tee takes its standard input and writes (or appends) it to one or more files, and also sends it to stdout:

run sim_0425 | tee sim_0425.log | tail -f

If you want to append to the log file instead, use the -a option:

run sim_0425 | tee -a sim_0425.log | tail -f
rici
  • 234,347
  • 28
  • 237
  • 341