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.