2

I am writing a bash script and would like the very last command to start as a separate process. The last command also sends all output to a file. I also, however, want the output to still appear on the console. What I have so far is,

$ command > "file" &

This sends the output to "file" and also starts the command as its own process. However, I also want to view the output in the console at the same time (but if I hit ctrl+c or w/e, the command doesnt stop). This is a lot like this question, but with the caviat that it needs to be its own thread.

I have tried:

$ command | tee "file" &

but the problem is that tee is than part of the process, and output doesn't actually appear..

So, just to clarify, I want to have command on its own process, sending output to a file, but still have the output appear in the console (until I hit q, enter, ctrl+c, or something). Since this is in a bash script, two separate lines would be acceptable.

Humdinger
  • 443

1 Answers1

2

It sounds like you want command to finish writing to the file, but you want to be able to interrupt the display to the console. I would take a different approach to the solution. In your script:

> "file"
command > "file" &
tail -n +1 -F "file"

Correction:

The original answer used -n 0, which initially outputs no lines of "file" but outputs any lines added to "file" after tail is started. This was not my intention--it was a mistake. I intended to use an option that would list all lines of "file" even if command had written some before tail was started. The correct option for that behavior is -n +1.

From the tail(1) man page:

   -n, --lines=K   output the last K lines, instead of the last 10;
                   or use -n +K to output lines starting with the Kth

The first line clears the contents of the file in case of a race condition where tail hits the file before command.

garyjohn
  • 36,494