1

I need to run a Python script on boot with display support

I have tried using rc.local, cron and a couple of other methods. All of them start the program and it runs as it should but I have a blank screen. I would like to be able to view what the program is doing on the screen. The script uses PyGame and printText to display a status to a small window. It works fine when I run it from a terminal window. I am using Raspbian on a Raspberry Pi 3.

Aurora0001
  • 6,308
  • 3
  • 23
  • 38
Ray
  • 11
  • 2

2 Answers2

1

2020 answer for those who finding this from google:
nowadays Raspbian is using systemd that can handle graphical output with its services. You can find a simple example running the internet browser on bootup. It should not be a big issue to replace ExecStart= of the browser with the call of your python script. For the example look at execute Python file on Systemstart.

Ingo
  • 42,107
  • 20
  • 85
  • 197
1

I'd output the script to a logfile and, if needed use tail -f on the file to check the output. But if you really want to direct the output directly, note that both cron and rc.local don't have a tty attached. This means that you need to redirect the output of the script to a tty. So in your crontab put something like:

*/10 * * * *  /home/pi/bin/myscript.py &> /dev/tty1

This still won't work (unless when you run the script as root), as the tty by default is owned by root. You can fix this by adding your user (probably pi) to the group tty: sudo usermod -a -G tty pi

Note that you can switch between your terminals with the ctrl-alt-f[1-6] keys (and from a X screen with ctrl+alt+F[1-6])

steviethecat
  • 261
  • 1
  • 13
  • steviethecat, Thanks for your timely reply. My script controls a large dual axis solar tracker so I can tell it is working properly and it also generates log files of what it is doing so I am 100% certain that it is executing properly. I added my user to the tty group and added the line – Ray Dec 29 '17 at 15:05
  • @reboot python /home/pi/Tracker1_6b.py &> /dev/tty1 to the crontab file but it does not execute the script. When I use the line: python /home/pi/bin/Tracker1_6b.py in the rc.local it runs the script but without screen support of course. – Ray Dec 29 '17 at 15:28
  • When I add the line / * * * python /home/pi/Tracker1_6b.py to the crontab file it does not run either. I can't say I have ever gotten it to execute from cron. I must have spent 6 hours by now trying to get this to work. – Ray Dec 29 '17 at 15:30
  • In rc.local you can put python /home/pi/Tracker1_6b.py &> /dev/tty1 & – steviethecat Dec 29 '17 at 17:02
  • I tried that and I still have a black screen with the program functioning normally other than the lack of display. I think my setup is pretty generic with a normal HDMI display. – Ray Dec 29 '17 at 18:54
  • Are you starting X automatically on your Pi or is it terminal only? – steviethecat Dec 30 '17 at 13:50