0

If I run such a bash script, and in the script, it will invoke some executable command, such as the following

myexec args1 arg2 > out.txt

myexec will print some log info through the c API printf. If I interrupt the script, because myexec is stuck by some unknown reason, does some log information will be lost, and not save to the out.txt file? In my experiment, I found it is, but is there any way to solve this? and I do not know why the log information not flush to the out.txt file

========

I use the stdbuf solved my problem, the related question is: Force line-buffering of stdout when piping to tee

Community
  • 1
  • 1
Djvu
  • 605
  • 1
  • 5
  • 18
  • Pretty much depends on a lot of things like buffer size, flush frequency, way printf is accessed, and most importantly your program, show the code. – asio_guy Sep 15 '15 at 05:22
  • Yes, I know, but I have no source code. I really want to know if whether I can have method to capture the log when I interrupt it. Such as I wrap the above code in the python fabric command. when I interrupt the execution, there is a exception, and I use a try...finally flush all the log info to the file. – Djvu Sep 15 '15 at 06:51

2 Answers2

3

When you interrupt the script, there's a good chance the stdout buffer isn't flushed. In your program, immediately after every printf, add an fflush(stdout) to flush the buffer. Alternatively, add a newline character (\n) at the end of your printf - that flushes the buffer too (but adds a newline to your log output).

tonysdg
  • 1,335
  • 11
  • 32
  • Thanks! I will use this in my program later. But I can not modify this executable now, because I can not modify the source code, I only know that it will log some information. – Djvu Sep 15 '15 at 04:34
  • In that case - try the answer to this question, which is a bit more similar to your current predicament: http://stackoverflow.com/questions/11337041/force-line-buffering-of-stdout-when-piping-to-tee – tonysdg Sep 15 '15 at 04:38
  • Newlines won't flush the buffer if stdout is redirected to a file. That'll switch to block buffering by default. – melpomene Sep 15 '15 at 07:00
  • @tonysdg thanks, from you given url, I find the method stdbuf works, how amazing! – Djvu Sep 15 '15 at 07:10
1

You can specify signal that will correctly finish your program (SIGUSR1, SIGUSR2 etc, though you can't override SIGKILL). In signal handler flush your output stream (fflush(stdout) or alternatives) and shutdown app. To stop your programm type smth like

kill -SIGUSR1 pidof myapp

Dmitrii Z.
  • 2,287
  • 3
  • 19
  • 29
  • This is definitely the more elegant way of handling the problem. A bit more complicated too. – tonysdg Sep 15 '15 at 04:10
  • In my server apps I write logs with fprintf(stdout,...) with overriding stdout and without any flushes. This make program run faster without removing logs. If I do need to read full logs - i use overrided SIGUSR1 that just flushes stdout. – Dmitrii Z. Sep 15 '15 at 04:13
  • Thanks very much ! Good point, but also only apply to you have the source code. – Djvu Sep 15 '15 at 06:54