LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-08-2013, 03:40 PM   #1
katesfb
Member
 
Registered: Nov 2006
Posts: 58

Rep: Reputation: 15
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.
 
Old 02-08-2013, 03:57 PM   #2
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 104

Rep: Reputation: 20
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
 
Old 02-08-2013, 04:01 PM   #3
katesfb
Member
 
Registered: Nov 2006
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
Old 02-08-2013, 04:08 PM   #4
lpwevers
Member
 
Registered: Apr 2005
Location: The Netherlands
Distribution: SuSE, CentOS
Posts: 104

Rep: Reputation: 20
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.
 
Old 02-08-2013, 04:52 PM   #5
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware, FreeBSD
Posts: 189

Rep: Reputation: 98
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

Last edited by Philip Lacroix; 02-08-2013 at 09:06 PM.
 
Old 02-08-2013, 08:08 PM   #6
katesfb
Member
 
Registered: Nov 2006
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
Old 02-08-2013, 09:03 PM   #7
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware, FreeBSD
Posts: 189

Rep: Reputation: 98
Quote:
Originally Posted by katesfb View Post
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

Last edited by Philip Lacroix; 02-08-2013 at 10:04 PM.
 
Old 02-10-2013, 04:18 PM   #8
katesfb
Member
 
Registered: Nov 2006
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
Old 02-10-2013, 08:18 PM   #9
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware, FreeBSD
Posts: 189

Rep: Reputation: 98
Hi,

Quote:
Originally Posted by katesfb View Post
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
 
Old 02-11-2013, 08:47 PM   #10
katesfb
Member
 
Registered: Nov 2006
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
Old 02-12-2013, 12:54 AM   #11
Philip Lacroix
Member
 
Registered: Jun 2012
Distribution: Slackware, FreeBSD
Posts: 189

Rep: Reputation: 98
Hi,

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

Quote:
Originally Posted by katesfb View Post
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

Last edited by Philip Lacroix; 02-12-2013 at 03:11 AM.
 
Old 02-12-2013, 04:01 AM   #12
katesfb
Member
 
Registered: Nov 2006
Posts: 58

Original Poster
Rep: Reputation: 15
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to close open ports using a python script or a shell script in python ?? apanimesh061 Programming 3 11-20-2011 12:31 AM
Python related: How to access a Perl script behind a firewall from Python? vxc69 Programming 8 12-14-2010 07:32 AM
How to autostart a bash script when DSL loads the Desktop?? linus72 Linux - Newbie 5 04-08-2009 01:12 PM
Make a desktop icon for python backup script OS X mannclay Programming 0 03-23-2009 09:46 AM
Where to put autostarting shell script? Ollir Mandriva 2 08-11-2004 02:30 PM


All times are GMT -5. The time now is 11:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration