问题
I now this has been asked before, but I have spent hours and hours trying to find out how to do this and absolutely nothing has worked. I have a python file that I want to automatically run in a terminal window after the pi has booted and loaded its GUI.
I don't know what else to do, and the annoying thing is I had it working for the same project (it took a long time to find out how then as well), but the pi crashed today and I can't remember how to do it again. All I can remember is that I added something to a file ending in /autostart, if that helps.
回答1:
I was able to launch a python file on startup by running sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
and adding @lxterminal -e python /home/pi/weatherStation/core.py
at the end of the file
回答2:
I had similar issues remembering where and how to do this. Then it all seemed to change again with Buster.
Having just freshly installed Buster onto a new SD card, I had to go through the whole process again. It's definitely easier the third or fourth time ;-)
I found that there was no lxsession folder in the path I had used before:
/home/pi/.config/lxsession/LXDE-pi/autostart
/home/pi/.config/lxsession did not exist in the Buster I just installed (May 2020)
I found a 2020 Buster related article here: http://wideberry.com/autostart-python-script-after-boot-in-raspbian-buster/ and from that found the autostart file in this path:
/etc/xdg/lxsession/LXDE-pi
Note: this is a root file and will affect all users.
As I had previously found an issue with timings of when to issue the python command, I call a script to pause before running my python scripts.
I added an @lxterminal
command in my /etc/xdg/lxsession/LXDE-pi/autostart
as follows:
lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@lxterminal -e /home/pi/StartCollectors.sh
@xscreensaver -no-splash
Note: as /etc/xdg/lxsession/LXDE-pi/autostart
this is a root protected file, it had to be edited with root powers, e.g. sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
StartCollectors.sh
contains this
#!/bin/bash
# started from the /etc/xdg/LXDE-pi/autostart file (reminder)
cd /home/pi/ETA2-copy
echo "starting House Collectors in 20 seconds"
sleep 10
echo "starting House Collectors in 10 seconds"
sleep 10
lxterminal --working-directory='/home/pi/ETA2-copy' --command='python3 Hiverun.py' -t 'Hive'
lxterminal --working-directory='/home/pi/ETA2-copy' --command='python3 ETALog.py' -t 'ETA'
lxterminal --working-directory='/home/pi/ETA2-copy' --command='python3 ETADailySum.py' -t 'Summary'
echo "My work is done. Closing in 10 seconds
sleep 10
exit
the -t options put a recognizable name on the terminal window.
It works.
BTW I collect data from an ETA PU15 boiler over my local Lan. I collect data from my British Gas Hive home control system and the UK Met Office and integrate it into an sqlite table so that I can track the house performance. Why? Why not? Call it a hobby.
I think this is my first answer on this forum. I hope it conforms and that it helps others.
来源:https://stackoverflow.com/questions/41913956/how-to-automatically-launch-python-file-once-gui-has-loaded-on-raspbian-pixel