LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Autostarting a python script when desktop loads (https://www.linuxquestions.org/questions/linux-software-2/autostarting-a-python-script-when-desktop-loads-4175449223/)

katesfb 02-08-2013 04:40 PM

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.

lpwevers 02-08-2013 04:57 PM

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

katesfb 02-08-2013 05:01 PM

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.

lpwevers 02-08-2013 05:08 PM

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.

radygrenek 02-08-2013 05:52 PM

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

katesfb 02-08-2013 09:08 PM

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.

radygrenek 02-08-2013 10:03 PM

Quote:

Originally Posted by katesfb (Post 4887387)
the python script i am using has the standard .py extension does this make any difference.

You're welcome! I guess it doesn't make any difference, as long as the script is executable. You're probably going to put something like this, in the local autostart file:

Code:

~/your_directory/script.py
or in the shell script I described in my post:

Code:

~/your_directory/script.py &
Kind regards,
Philip

katesfb 02-10-2013 05:18 PM

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
#
#~/.lxde-autostart

/usr/bin/lxterminal &    #this works fine by either double clicking on .lxde-autostart or reboot

/usr/bin/idle &  #this works fine by double clicking on .lxde-autostart but get error on reboot see above

#~piShutdown.py & #no good

#/usr/bin/lxterminal python & #no good

#lxterminal python & #no good

#/usr/bin/lxterminal python piShutdown.py & #no good

#/usr/bin/lxterminal python /home/pi/piShutdown.py & #no good

#/usr/bin/lxterminal sudo python /home/pi/piShutdown.py & #no good

I have tried both having the py script made executable and non-executable but result is the same.

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.

radygrenek 02-10-2013 09:18 PM

Hi,

Quote:

Originally Posted by katesfb (Post 4888677)
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.

lxterminal needs a --command option (see below); you can also use xterm. The examples below should work.

Code:

#!/bin/bash
#
#~/.lxde-autostart

# Running with lxterminal:
/usr/bin/lxterminal --command ~/path/script.py &
/usr/bin/lxterminal --command python ~/path/script.py &

# Running with xterm:
/usr/bin/xterm ~/path/script.py &
/usr/bin/xterm -e python ~/path/script.py &

# Running the script without opening a terminal:
~/path/script.py &
python ~/path/script.py &

If you need root permissions to run the py script, you'll have to make your .lxde-autostart to run as root as well. Unfortunately I don't have knowledge of IDLE, so I cannot say much about it.

Kind regards,
Philip

katesfb 02-11-2013 09:47 PM

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.

radygrenek 02-12-2013 01:54 AM

Hi,

you're welcome, I'm glad that it works. :)

Quote:

Originally Posted by katesfb (Post 4889548)
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

If I understand your question correctly, you could try to run your script from IDLE using the -r option, followed by the script name:

Code:

#!/bin/bash

/usr/bin/idle -r ~/path/script.py &

I hope that it works. Here is the man page:
http://manpages.ubuntu.com/manpages/...thon2.5.1.html

Kind regards,
Philip

katesfb 02-12-2013 05:01 AM

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.