1

I am looking for a help regarding a shell script to redirect the output of a command to a file. I have a C program that reads the input from a serial port and display. I want this data to be redirected to a file. I am executing this from a java program by calling

Runtime r = Runtime.getRuntime();
Process procObj = r.exec("sh " + scriptfile);

I have tried writing the script file as

./program >> file.txt

The file.txt is not getting updated. Here, the program doesn't end until the connection to the port is lost, in a sense it is infinitely running. So my program keeps looking for data on the port and display as and when it is there. I just need to redirect the same output to a file that I would use as a log. I looked at How to make shell output redirect (>) write while script is still running? but not helpful.

Kindly help..

Community
  • 1
  • 1
user_abh
  • 357
  • 3
  • 6
  • 20
  • consult to this answer: http://stackoverflow.com/questions/8007871/how-to-run-a-perl-file-using-java/8008156#8008156 – bpgergo Nov 08 '11 at 11:17
  • I am sorry I cud not understand which part in the link above applies to my question. I am able to call the script file and execute it. If I give a normal echo command it works and the output is redirected to the file quite well. As I said my C program never ends and the output needs to be continuously updated in the file. – user_abh Nov 08 '11 at 11:27
  • But when program finally terminates, the file *gets* updated, isn't it? – jpalecek Nov 08 '11 at 11:27
  • I modified my script file as 'stdbuf -oL ./program >>file.txt'. This is only adding a bit of the output to the file. I don't know if am missing something here. Basically, I need the shell script to infinitely keep updating the text file with the output generated by the C program. Any help? Thanks. – user_abh Nov 08 '11 at 12:03

4 Answers4

2

How much output does program generate? Using standard IO redirection will add a 4KB buffer between stdout and file. This means your program must output more than 4KB of data before the OS starts to write to the file.

To fix this, add stdout.flush() to your program when a "work unit" is complete (maybe a line but might be more than one line).

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Why do you think that "standard IO redirection" adds any buffers? – hagello Dec 17 '14 at 09:38
  • 1
    "the OS starts to write to the file": The libc does the buffering in user space on order to economize on system calls. – hagello Dec 17 '14 at 10:08
  • 1
    I/O redirection is a feature of the shell. libc detects the type of the stdin/stdout handles and sets the buffering accordingly (none for unknown types, line buffered for cooked terminals, 4KB for files and pipes). – Aaron Digulla Dec 17 '14 at 15:45
1

Can you try ./program >> file.txt 2>>file.txt, or ./program 2>&1 >>file.txt?

apaderno
  • 28,547
  • 16
  • 75
  • 90
maur
  • 11
  • 1
1

just try this

    List<String> cmd = new ArrayList<String>();
    cmd.add("sh");
    cmd.add("-c");
    cmd.add("program 1> file.txt 2>&1");

    ProcessBuilder pb = new ProcessBuilder(cmd);
    Process p = pb.start();
Zac
  • 51
  • 2
0

If you use standard C calls for output (printf, puts etc.), your output may get buffered. On C89 and onwards, it depends on the buffering mode (unbuffered, fully buffered, line buffered) and on the size of the buffer, whether your output is buffered at all and when the buffer is flushed (see http://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html and man setvbuf).

By default, output to a file is fully buffered on Linux. If you want the output to appear immediately in the output file, you may:

This behaviour is not related on the fact the you start your C program in a Java program via a shell script. This behaviour depends on the standard C library that you have linked into your program.

Community
  • 1
  • 1
hagello
  • 2,843
  • 2
  • 27
  • 37