Search code examples
pythonflaskraspberry-pi

Running Flask App with GUI after boot on Raspberry PI


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+


Solution

  • 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.