Raspberry Pi : Auto Run Gui On Boot
Solution 1:
Without knowing you Pi setup it's a bit difficult. But with the assumption you're running raspbian with its default "desktop" mode:
- Open a terminal on your Pi, either by
ssh
ing to it or connecting a monitor/keyboard. - First we need to allow you to login automatically, so
sudo nano /etc/inittab
to open the inittab for editing. - Find the line
1:2345:respawn:/sbin/getty 115200 tty1
and change it to#1:2345:respawn:/sbin/getty 115200 tty1
- Under that line, add
1:2345:respawn:/bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
. Type Ctrl+O and then Ctrl+X to save and exit - Next, we can edit the rc.local.
sudo nano /etc/rc.local
- Add a line
su -l pi -c startx
(replacingpi
with the username you want to launch as) above theexit 0
line. This will launch X on startup, which allows other applications to use graphical interfaces. - Add the command you'd like to run below the previous line (e.g
python /path/to/mycoolscript.py &
), but still above theexit 0
line. Note the&
included here. This "forks" the process, allowing other commands to run even if your script hasn't exited yet. Ctrl+O and Ctrl+X again to save and exit.
Now when you power on your Pi, it'll automatically log in, start X, and then launch the python script you've written!
Also, my program requires an internet connection on execution but pi connects to wifi later and my script executes first and ends with not connecting to the internet.
This should be solved in the script itself. Create a simple while
loop that checks for internet access, waits, and repeats until the wifi connects.
Solution 2:
Two steps on Raspian:
- Make sure you boot into GUI (can setup via sudo
raspi-config
) - Edit
~/.config/lxsession/LXDE-pi/autostart
and add your python script to the path: e.g.@python /home/pi/your_script.py
It depends on the version of raspian if the path is
~/.config/lxsession/LXDE-pi/autostart
or
~/.config/lxsession/LXDE/autostart
I recommend trying one at a time.
(Older version might use this path /etc/xdg/lxsession/LXDE-pi/autostart
(ref))
This should run the script after the UI initialises but you don't have any guarantee WiFi is connected though. I recommend ammending your python script to check if it's connected first and if not retry after a few seconds until it is, then execute the rest as expected.
Post a Comment for "Raspberry Pi : Auto Run Gui On Boot"