![]() |
Autostarting a python script when desktop loads
Hi,
I have been trying to get a python script to automatically start when the desktop loads which would be after the login although my system automatically boots to the desktop. I have tried using a .desktop file located in ~.config/autostart (sometimes in conjunction with a shell script) to do it and although i can get it to execute it still wont automatically start on reboot. Is there a definitive easy way to do this. What i have is a python script that resides in a directory under my home directory, the python file sets up a GUI that accesses two other files that are in the same directory. Once going the py script runs indefinitely until it is stopped. Any help is much appreciated. Cheers. |
Hi,
Which desktop do you run? If it's KDE I might be able to help you. If it's Gnome, I'm afraid you're going to have to wait till someone else comes by. Louis |
Hi,
And thanks for the reply. Principally i would like it to run in LXDE but being able to auto start a python script in Gnome, KDE or whatever would also be an advantage. Cheers. |
Well, in that case, for KDE, open your System Settings, and navigate to Startup and Shutdown. There in Autostart you have the option to either add programs or scripts. I believe the Add Script button on the right will help you out.
Louis. |
Hi,
I posted something here about autostarting an application in Lxde, maybe you'll find it useful: http://www.linuxquestions.org/questi...ux-4175448789/ [Edit] A simpler (the) solution is to use the ~/.config/lxsession/LXDE/autostart file, if you have it in your home directory (I don't). Kind regards, Philip |
Hi,
And thanks for the replies. Philip - this sounds like exactly what i need - the python script i am using has the standard .py extension does this make any difference. I will set it up and let you know how i go. Cheers. |
Quote:
Code:
~/your_directory/script.py Code:
~/your_directory/script.py & Philip |
Hi Philip,
Thanks for the reply and all your help so far. I seem to be having a few issues getting python scripts to auto start. My .lxde-autostart has the following entries in it (see below). The commented out entries are the python scripts that wouldnt work. Idle works if i double click on the .lxde-autostart file and does auto start on reboot but comes up with an error indicating that a connection could not be made. The call to lxterminal works fine. Code:
#!/bin/bash If i start lxterminal then issue the command python script.py, this works fine. I am trying to run this on a raspberry pi which is set to boot to desktop and runs headlessly such that i talk to it via tightVNC on a laptop. This shouldnt make any difference should it since it works fine for some things just not py scripts. If i run the py script from IDLE or python from command line everything works perfectly but i need it to have the scripts auto start. I find it odd that if i run python from the command prompt it works but if a i put lxterminal python in .lxde-autostart, it doesnt. Your help is much appreciated. Cheers. |
Hi,
Quote:
Code:
#!/bin/bash Kind regards, Philip |
Hi Philip,
IT WORKS!! Many many many thanks for all your help. I had two python scripts that i needed to auto start, the first one worked fine (simple non-Gui script). The second script took a bit more effort to get it to work and i am not quite sure why that was, i think that becos it was starting while the desktop was still loading and becos it set up a GUI as part of its operation maybe there was some sort of conflict there maybe (so i put in a sleep command to prevent the script loading until the desktop had fully loaded) it also may have been due to the fact that i was using the sudo command (have to use this with the first script as it accesses IO pins on the raspberry pi which requires root access). Anyway all seems good now - assuming it keeps going. Quick question: is there a method for simulating key presses. For example another way of auto starting the second script was to Auto start the python IDE only then simulate a key press to open a file and execute it Cheers. |
Hi,
you're welcome, I'm glad that it works. :) Quote:
Code:
#!/bin/bash http://manpages.ubuntu.com/manpages/...thon2.5.1.html Kind regards, Philip |
Hi Philip,
Thanks for the reply - i actually tried this approach a while ago to get it to work by auto starting idle as you suggest, mainly for the second of the two scripts that i have to auto start but it wouldn't execute the file, just opened the shell window. However i might re-investigate this approach as, although both of the scripts are now working from autostart the one that sets up a GUI is a bit unstable and tends to stop after a while. Not quite sure why this is given the other script is fine and the GUI script is very stable when run normally from IDLE. I'll let yi know how i get on. What i actually meant by simulating key presses was to set up some sort of macro holding the key presses that would open a file in IDle then execute it however i think this is more hassle than it is worth. Your help is much appreciated. Cheers. |
All times are GMT -5. The time now is 09:51 PM. |