I have tried two methods to direct the output of a program's printf
calls to a file. In both cases, sometimes the file is updated immediately, sometimes it takes several minutes, and sometimes it doesn't update the file at all.
My program runs from a bash script that is executed by /etc/network/if-up.d/upstart. I have tried this for my bash script:
#!/bin/bash
sleep 2
if ! pgrep "demo" > /dev/null
then
sudo /home/pi/code/demo_device/demo >> /home/pi/code/demo_device/auto_log.txt 2>&1 &
fi
and this:
#!/bin/bash
exec 1> >(logger -s -t $(basename $0)) 2>&1
sleep 2
if ! pgrep "demo" > /dev/null
then
sudo /home/pi/code/demo_device/demo &
fi
The auto_log.txt file in the first case and the /var/log/user.log file in the second case both have the issues that I described above. I also had the bash script run the demo executable with start-stop-daemon
and I got the same results. How can I have a log file update immediately and consistently when running a program as a background process on boot-up?
note: I don't know if this is relevant, but the program is designed to run continuously and never exit.
fflush
for when I can modify it, though. I upvoted but I didn't mark it as the answer because I'm holding out hope that someone knows how to solve the issue from outside of the program. – Jason Nov 11 '16 at 20:06fflush
itsstdout
-- but unfortunately, since the program is not designed to be terminated, then it very well might not. It is allowed to hold on to its buffer and not output it, for as long as it doesn't get more data to fill it up. – Alex Jul 12 '17 at 16:53