3

I wrote a small C executable that uses "printf" statements for debugging, like

printf("Starting up...\n");

When I run the executable in a Terminal window with ./myProgramme all the printf output appears in the Terminal window, fine so far.

But I want to autostart the executable and added the line

@./myProgramme

to the autostart file of lxsession (.config/lxsession/LXDE-pi/autostart)

After booting the Pi, the executable is running (I checked with ps -A), but I can't find the printf output anywhere I looked (/var/log/messages, /dev/stdout, /dev/stderr)

I also tried changing the autostart line to

@./myProgramme > /home/pi/myLog.txt

but no "myLog.txt" file is created after booting...

1

OK, after some hours of trial & google I found my answer:

First, the lxsession autostart does not work like bash, so it ignores the "> /home/pi/myLog.txt" part of the line

When using /etc/rc.local to autostart myProgramme it kind of worked (it created the myLog.txt file), but the logfile contained no outputs from the printf commands.

I found the reason for that to be that my programme never terminates (it runs in an endless loop, monitoring in the background), so apparently in this case the "printf"-messages are only sent to stdout when the stdout-buffer is full (4K of text), even though all printf messages where terminated by a newline "\n". That's why I didn't see outputs in the myLog.txt file, at least not when I tested it, as the messages never accumulated to >4K size

After putting the line

setbuf(stdout, NULL);

into my C code and recompiling, the stdout buffer is disabled and now I see the printf-output in myLog.txt :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.