Bash output stream is written to a file

so I run this on bash:

# somedevice -getevent 

What this command does, it just continues to work, and every time my device sends certain data, let's say it detects a temperature change, it displays something like this

 /dev/xyz: 123 4567 8910112238 20 /dev/xyz: 123 4567 8915712347 19 /dev/xyz: 123 4567 8916412345 22 /dev/xyz: 123 4567 8910312342 25 /dev/xyz: 123 4567 8910112361 18 /dev/xyz: 123 4567 8910112343 20 

And it just keeps working, and as soon as it has some reason, it outputs something. Thus, there is no end to execution.

No, the echo works fine, however, when I try to use the '>' operator, it doesn't look like writing to a file.

for example,

 #somedevice -getevent > my_record_file 

this does not work properly, my_record_file only gets the data written to it in intervals , however I want it to be written immediately.

Any ideas?

+4
source share
4 answers

The output is buffered because the standard C library changes the output buffering mode depending on whether stdout is a terminal device or not. If this is a terminal device (according to isatty(3) ), then stdout is buffered by line: it is cleared every time a newline is written. If this is not a terminal device, then it is completely buffered: it is cleared only when a certain amount of data is written (usually something on the order of 4 KB to 64 KB).

So, when you redirect the output of a command to a file using the shell redirection operator > , it no longer outputs to the terminal, and it buffers its output. The program can change the buffering mode of setvbuf(3) and friends, but the program must cooperate for this. Many programs have command line options to make them string buffers, for example. grep(1) --line-buffered . See if your team has a similar option.

If you do not have this option, you can try to use a tool, for example unbuffer(1) , to disable the output stream, but it does not always work and is not a standard utility, so it is not always available.

+5
source

The somedevice command probably uses the Standard I / O Library, and buffering is enabled by default in this library. It turns off when the output reaches the terminal / console.

Can you change somedevice ? If not, you can still hack it. See http://www.pixelbeat.org/programming/stdio_buffering/ for more details.

+2
source

You can try "tee":

 somedevice -getevent | tee -a my_record_file 

The '-a' option is to add, not just replace content.

0
source

Perhaps this is due to the fact that the stdout " somedevice -getevent " somedevice -getevent is blocked. According to this , stdout is by default a string buffer (that is, what you want) if stdout is a terminal and is otherwise blocked by the buffer.

I would look at the manual for your somedevice command to make sure that you can force the output to be unbuffered or line buffered. If not, stdbuf -oL somedevice -getevent > my_record_file should do what you want.

0
source

Source: https://habr.com/ru/post/1402006/


All Articles