Running Flask App with GUI after boot on Raspberry PI

Question:

I’m trying to get a Flask App with a GUI with Tkinter to run on a Raspberry Pi after boot but I can’t seem to get it working. I’ve tried to make it run on boot by putting an sh script inside /etc/init.d/ with the following instructions:

#!/bin/bash
### BEGIN INIT INFO
# Provides:          myapp
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: My Flask App
# Description:       My Flask App with Tkinter GUI
### END INIT INFO

case $1 in
    start)
        cd /home/pi/
        python3 script.py &
        ;;
    stop)
        pkill -f 'python3 script.py'
        ;;
    *)
        echo "Usage: /etc/init.d/myapp.sh {start|stop}"
        exit 1
        ;;
esac

exit 0

And then I ran the following command:
sudo update-rc.d myapp.sh defaults

If it helps here’s a pastebin with my code: https://pastebin.com/N6QEdhUg

My Raspberry Pi is a Model 3 B+

Asked By: Rui

||

Answers:

Have you tried running it through crontab? An example entry would be:

@reboot /usr/bin/python3 /path/to/script.py

Answered By: Devin Gardner

Eventually managed to get it working, here’s what I did:

First I created an sh script named AutoStart in my user directory with the following lines:

#!/bin/bash

cd /home/pi
python3 script.py

I then edit the autostart file with: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

And then I write on the bottom: @sh /home/pi/AutoStart.sh

After that I rebooted and it worked fine.

Answered By: Rui
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.