Redirect stdout to a file when process is run in background

7,204

Solution 1

What about sh -c './program > file.txt; cat file.txt' & ?

Solution 2

When redirecting stdout to a text file, you are changing the stream type from console to file. The console output is unbuffered, while the file output is buffered. Until the program flushes the stdout device (file), the text will continue to buffer until the buffering threshold is reached. If the text "output\n" is printed every 2 seconds, and the threshold is 4,096 bytes, it would take nearly 20 minutes to see any output from the program in the file.

Share:
7,204
Rauffle
Author by

Rauffle

Updated on September 18, 2022

Comments

  • Rauffle
    Rauffle over 1 year

    How can I redirect the stdout of a program to a file when it's run in the background?

    I have a program that generates output every second. When I run it normally and redirect to a file, the output is in that file as expected:

    #./program > file.txt
    #cat file.txt
     output
     output
     output
    #
    

    When I try to do the same thing in the background, the file will remain empty:

    #./program > file.txt &
    #cat file.txt
    #
    
    • slhck
      slhck almost 12 years
      Not the way I expect it to be. What program are you running?
    • Rauffle
      Rauffle almost 12 years
      The program itself is irrelevant but is a simple executable that checks something every second and writes the current value to stdout.
    • Alex Allen
      Alex Allen almost 12 years
      Is it also empty when the program is finished?
    • speakr
      speakr almost 12 years
      @Rauffle I guess the program itself may be not irrelevant. Normally, you should have the content in your file. Are you sure the program outputs to STDOUT and not e.g. to STDERR?
    • garyjohn
      garyjohn almost 12 years
      Does the jobs command show that your program is running? It may be stopped for some reason.
    • Rauffle
      Rauffle almost 12 years
      Tried with another program that prints to stdout and it's the same thing. If I direct stdout to a file but run it in the foreground the file will have content. If I run it in the background, the file remains empty even after the program finishes running. I'm rather new to CentOS, is this perhaps some kind of 'feature' of the OS?
    • chepner
      chepner almost 12 years
      Can you give us more detail on the actual program you are running? Ordinarily, running in the background should make no difference.
    • billc.cn
      billc.cn almost 12 years
      Maybe your program needs to take input from StdIn and crashed because it's not connected?
    • Xiè Jìléi
      Xiè Jìléi almost 12 years
      What about sh -c './program > file.txt; cat file.txt' & ?
    • Rauffle
      Rauffle almost 12 years
      @Xie I've since found a workaround to achieve what I wanted, but this worked (modified to "sh -c './program > file.txt' &"). Want to submit it as an answer?
    • Xiè Jìléi
      Xiè Jìléi almost 12 years
      @Rauffle Done! :)
  • singpolyma
    singpolyma over 8 years
    This does not work for me
  • singpolyma
    singpolyma over 8 years
    stackoverflow.com/a/11337310/8611 with tee worked. Not sure why buffering is different in background than foreground