2

I have program built for a drone project where my pi needs to start reading when the pi is booted up. I have substituted a simple C program instead call myloopprogram located at the root folder ~. It prints out "running..." every 4 seconds around. But I am unable to get this to run and see the output at bootup. What am i doing wrong?

I have tried editing /etc/rc.local with program located at root~:

./myloopprogram &
exit 0

I have tried crontab -e:

@reboot ./myloopprogram &

I have tried creating a simple text file creator and put it in init.d and edited into /etc/rc.local. Neither created a text file anywhere I could find. I then tried crontab -e for the text file creator as:

@reboot ./textfilecreator

and finally something worked and a text file was created at root. So how do I get the while loop to work or at least crontab to run my infinite loop?

PS: I am expecting an output printing to console at boot or at least a job listed on "~ fg" command. Neither show.

1
  • 1
    Similar questions have been asked dozens of times. Which answers have you looked at?
    – joan
    Commented Dec 8, 2017 at 11:58

1 Answer 1

3

If you want the output printed to console, you are going to have to set that up. There are lots of things started at boot, and they can't all print to console or it would be an interleaved mess. So, just as none of them can hog the foreground without special arrangment, none of them get to hog the console used by the kernel and init (on current versions of Raspbian, init is systemd). The default for anything written to such a process's stdout or stderr is systemd's "journal" (i.e., the system log); see "StandardOutput" in man systemd.exec. That option can be set to "console" (note it must be combined with something else); this may be what you want if it is only a short message every 4 seconds, but it means you will have to start the process via systemd (which is actually the best way to start a boot service anyway, but involves a little more work than rc.local or cron).

However, the console used by kernel and init is not the only text terminal.

I'm presuming you are running sans GUI (i.e., "text mode") but not headless (i.e., you have a screen and keyboard). In that case, you could create a shell wrapper and redirect the output to one of the other virtual terminals (VTs). These are where the logins you can find via Ctrl-Alt-F[n] are, where "[n]" is 1-6. Note 2-5 will look the same (except for the tty number shown), since no kernel or journal output is going there (except for supposedly important warnings).

There's actually more than 6 VTs, but only the first six have a login running. That won't stop you from writing to them but it may be tidier to use tty7 instead. So you could create a shell wrapper like this and put that in rc.local (or it's own script and start it however you want):

(
    exec myloopprogram > /dev/tty7 2&>1
) & 

The wrapper here is just the subshell parentheses; this way the subshell will fork to the background (final &) and then replace itself (exec) with myloopprogram, redirecting (>) all output (2&>1) to /dev/tty7. If at boot you then switch to that VT you should see the output.

You could of course just use /dev/tty1 if that's what you really want, or you do not have a keyboard connected. If you don't, but want to use an alternate terminal, you could put the subshell in rc.local and then afterward:

chvt 7

Which should switch the screen view to to tty7 (though I can't promise it will stay there).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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