Running a Python Script AFTER X GUI Starts up?
I've been looking for a way to run a script when my pi boots. But everything I find is for running scripts before it loads into my GUI (X - It's running Raspbian atm.)
Is there a good way to run a script after X loads up?
Thanks
-Nick
Is there a good way to run a script after X loads up?
Thanks
-Nick
Re: Running a Python Script AFTER X GUI Starts up?
Start LXTerminal (from the "Start menu").
ghans
ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
Re: Running a Python Script AFTER X GUI Starts up?
Perhaps I wasn't clear. I know how to run a script. I'm asking how to AUTOMATICALLY run a script after X starts up.
Re: Running a Python Script AFTER X GUI Starts up?
You can start automatically your script adding a new line to the file /etc/xdg/lxsession/LXDE/autostart. This new line should start with "@" + scriptname (including path to script if necessary):
Code: Select all
sudo nano /etc/xdg/lxsession/LXDE/autostart
Code: Select all
@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
@xscreensaver -no-splash
@/pathtomyscript/myscript
Re: Running a Python Script AFTER X GUI Starts up?
Thanks for the reply, but that didn't seem to work for me.
@/home/pi/Desktop/blah.py
Yes?
When I reboot, X loads as normal, but the script doesn't run.
@/home/pi/Desktop/blah.py
Yes?
When I reboot, X loads as normal, but the script doesn't run.
Re: Running a Python Script AFTER X GUI Starts up?
shouldn't that beTumerboy wrote:Thanks for the reply, but that didn't seem to work for me.
@/home/pi/Desktop/blah.py
Yes?
When I reboot, X loads as normal, but the script doesn't run.
Code: Select all
@/usr/bin/python /home/pi/Desktop/blah.py
You need to call the script the same way as if you were typing it yourself into the terminal
{sig} Setup: Original version Raspberry Pi (B, rev1, 256MB), Dell 2001FP monitor (1600x1200), 8GB Class 4 SD Card with Raspbian and XBMC, DD-WRT wireless bridge
-
- Posts: 12
- Joined: Wed Aug 29, 2012 12:42 pm
Re: Running a Python Script AFTER X GUI Starts up?
I too am trying to get a python programme to run on startup of LXDE. At the moment, I'm keeping it really simple with a 'hello world' programme to begin with. Having followed all of the instructions above, once LXDE loads up, nothing happens.
The contents of the etc/xdg/lax session/LXDE/autostart is:
@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
@xscreensaver -no-splash
@usr/bin/python /home/pi/test1.py
I have only added the last line and all the file names are correct. Anyone got some clues to help me??!
The contents of the etc/xdg/lax session/LXDE/autostart is:
@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
@xscreensaver -no-splash
@usr/bin/python /home/pi/test1.py
I have only added the last line and all the file names are correct. Anyone got some clues to help me??!
Re: Running a Python Script AFTER X GUI Starts up?
I had similar experience but added sudo in front and it ran OK. Mine is a radio/mp3 app.
Re: Running a Python Script AFTER X GUI Starts up?
@crazyideas: Are you missing a slash in front of 'usr/bin'?
Re: Running a Python Script AFTER X GUI Starts up?
working code:
sudo nano /etc/xdg/lxsession/LXDE/autostart
edit :add last line
@/usr/bin/python /home/pi/script.py
sudo nano /etc/xdg/lxsession/LXDE/autostart
edit :add last line
@/usr/bin/python /home/pi/script.py
-
- Posts: 15
- Joined: Wed Jan 09, 2013 6:36 am
Re: Running a Python Script AFTER X GUI Starts up?
GIVE THIS MAN A BELLS!!!
Re: Running a Python Script AFTER X GUI Starts up?
A. I'm working with the camera module and wondering about a similar scenario. Is it necessary to have something in the camera config/init or whatever file to have this start at boot? I'm using a sensor which then fires the camera and I may want to have this all start when I boot up. I will want to run without a monitor at some stage and was thinking that I would have to used a remote/wifi keyboard and work 'blind' to get things happening - possibly by having the script/programme on the desktop to reduce errors when I cd to it.
Any thoughts please
cheers
Any thoughts please
cheers
I seemed to have earned a reputation as being somewhat less than intact, both hardware and software tests are, as yet, inconclusive as to the reason for this. Syntax errors in the original programming are perhaps the most likely cause.
Re: Running a Python Script AFTER X GUI Starts up?
THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Re: Running a Python Script AFTER X GUI Starts up?
I've tried this by adding @/usr/bin/python /home/pi/script.py to the autostart file but nothing happensashwin8 wrote:working code:
sudo nano /etc/xdg/lxsession/LXDE/autostart
edit :add last line
@/usr/bin/python /home/pi/script.py
Anything else I need to do ?
Gordon77
Re: Running a Python Script AFTER X GUI Starts up?
Got it to work with editting...
/etc/xdg/lxsession/LXDE-pi/autostart
added:
@/usr/bin/sudo /home/pi/script.py
As my python script is using RPI.GPIO it needs sudo so I modified the python script to start with
#!/usr/bin/env python
then in lxterminal do 'chmod +x script.py', which worked on my Pi2B but on my Pi1Bv1 needed 'sudo chmod +x script.py'
and then reboot.
So what I thought would be a 2 minute job took quite a while to research.
Thanks to all who have posted in the past on this topic that I managed to Google.
To shut it down I added a switch between GPIO pins 25 & 26 and the following code to the python script
Gordon77
/etc/xdg/lxsession/LXDE-pi/autostart
added:
@/usr/bin/sudo /home/pi/script.py
As my python script is using RPI.GPIO it needs sudo so I modified the python script to start with
#!/usr/bin/env python
then in lxterminal do 'chmod +x script.py', which worked on my Pi2B but on my Pi1Bv1 needed 'sudo chmod +x script.py'
and then reboot.
So what I thought would be a 2 minute job took quite a while to research.
Thanks to all who have posted in the past on this topic that I managed to Google.
To shut it down I added a switch between GPIO pins 25 & 26 and the following code to the python script
Code: Select all
GPIO.setup(26,GPIO.IN,pull_up_down = GPIO.PUD_UP)
if GPIO.input(26) == 0:
os.killpg(p.pid, signal.SIGTERM)
path = 'sudo shutdown -h now '
os.system (path)
Re: Running a Python Script AFTER X GUI Starts up?
I had a similar problem and could not find this solution on any forum. I needed to run a shell script in GUI mode (xterminal) upon login to the graphic enviroment (LXDE). It took me a few hours of trials and errors but it worked:
1. Edit /etc/xdg/lxsession/LXDE-pi/autostart
2. Append this line:
3. Make sure you have at least read and execute permissions to all paths in /path/to/your/script.sh and the script itself
It runs on console automatically when the pi boots up to the graphic environment.
NOTE: I also installed xrdp for remote access from a Windows machine. When I login remotely via RDP, my script runs again. I need only one instance running (on the console), so I have to validate on my script that there aren't any other instances already running:
I hope this helps somebody else.
Peace \m/
1. Edit /etc/xdg/lxsession/LXDE-pi/autostart
2. Append this line:
Code: Select all
@lxterminal -e /path/to/your/script.sh
It runs on console automatically when the pi boots up to the graphic environment.
NOTE: I also installed xrdp for remote access from a Windows machine. When I login remotely via RDP, my script runs again. I need only one instance running (on the console), so I have to validate on my script that there aren't any other instances already running:
Code: Select all
#!/bin/bash
[ -z "`pgrep $0`" ] && exit
# my code below
:
:
Peace \m/
Re: Running a Python Script AFTER X GUI Starts up?
This is very helpful !
Thank you!
Thank you!
Re: Running a Python Script AFTER X GUI Starts up?
Sorry to resurrect an old thread.
I have a python2 application using the camera interface and tkinter on a Pi2 running an up to date Jessie.
I have added the path to autostart (LXDE-pi) as suggested and chmod +x but no matter what, I end up with a normal GUI after boot with no sign of my graphical app.
Is what I am trying to do even possible?
I am not a native Linux user nor indeed a python programmer so please be gentle
I have a python2 application using the camera interface and tkinter on a Pi2 running an up to date Jessie.
I have added the path to autostart (LXDE-pi) as suggested and chmod +x but no matter what, I end up with a normal GUI after boot with no sign of my graphical app.
Is what I am trying to do even possible?
I am not a native Linux user nor indeed a python programmer so please be gentle

Re: Running a Python Script AFTER X GUI Starts up?
If using Jessie use ~/.config/lxsession/LXDE-pi/autostart
Re: Running a Python Script AFTER X GUI Starts up?
Many thanks - worked a treat.
Re: Running a Python Script AFTER X GUI Starts up?
Editing the file ~/.config/lxsession/LXDE-pi/autostart did the trick however I can't run the script as sudo.
When I run
It runs great.
But-
Or
Don't work.
Any ideas?
When I run
Code: Select all
@gnome-terminal -x bash -c "/home/pi/test"
But-
Code: Select all
@gnome-terminal -x bash -c "sudo /home/pi/test"
Code: Select all
@gnome-terminal -x bash -c "/usr/bin/sudo /home/pi/test"
Any ideas?
Re: Running a Python Script AFTER X GUI Starts up?
If you tried to run that as sudo from the command line as a normal user, it would need to be a sudoable user and it would ask for the password, at least it does normally, I'm not sure what the latest view from the Raspbian people is. Also, doesn't the -x turn on bash debugging? I don't know what happens when you do that from the autostart LXDE file. Also, if you are running as root, you might not have the right .Xauthority magic although I think that is more of a known problem to me when I ssh -X in and then change to running something root.nlandau wrote:But-OrCode: Select all
@gnome-terminal -x bash -c "sudo /home/pi/test"
Don't work.Code: Select all
@gnome-terminal -x bash -c "/usr/bin/sudo /home/pi/test"
Re: Running a Python Script AFTER X GUI Starts up?
found my answer here:
viewtopic.php?f=91&t=65607&p=1001600&hi ... #p1001600
viewtopic.php?f=91&t=65607&p=1001600&hi ... #p1001600
11. **IMPORTANT** DO NOT add this line as the last line after the @xscreensaver line or you will never see it or execute it. (took a while to figure that out)
Re: Running a Python Script AFTER X GUI Starts up?
I tried all suggested solutions but unfortunately.
None runs.
I had to launch a python script after UI environment.
After some hours I found this way, in two steps:
ONE)
1) from UI menu
choice "Preferences" and "Main Menu Editor"
2) In "Main Menu Editor" window
select "Preferences" on the left
end ENABLE "Default applications for LXSession" on right side
3) Now you can see "Default applications for LXSession" item in UI Main menu!!
TWO)
4) Finally launch "Default applications for LXSession" item in UI Main menu
5) In LXSessionconfiguration window...
select "Autostart" panel on the left
6) on the right side you can "Add" with textfield and button your desidered command line!!
7) test shutting down raspberry and see..
... hoping to be useful for anyone...
None runs.
I had to launch a python script after UI environment.
After some hours I found this way, in two steps:
ONE)
1) from UI menu
choice "Preferences" and "Main Menu Editor"
2) In "Main Menu Editor" window
select "Preferences" on the left
end ENABLE "Default applications for LXSession" on right side
3) Now you can see "Default applications for LXSession" item in UI Main menu!!
TWO)
4) Finally launch "Default applications for LXSession" item in UI Main menu
5) In LXSessionconfiguration window...
select "Autostart" panel on the left
6) on the right side you can "Add" with textfield and button your desidered command line!!
7) test shutting down raspberry and see..
... hoping to be useful for anyone...
Re: Running a Python Script AFTER X GUI Starts up?
An issue I am having here that is odd.
Scenario: I have a .sh script that launches a python script after changing to proper directories, and this fires up on start of X per instructions above.
On boot, everything appears to work fine, except.... No sound from my script. However, if I exit the script and manually run the .sh to start things the same way, my sound works perfectly.
NOT using a USB device, just the 3.5mm out built in to the raspberry. Near as I can figure maybe this is launching and prepping sounds before alsa sound is fully loaded in? But I would think I would get errors from my Python script if that was the case.
It is likely something I am overlooking. Any tips as to what that may be would be appreciated.
Scenario: I have a .sh script that launches a python script after changing to proper directories, and this fires up on start of X per instructions above.
On boot, everything appears to work fine, except.... No sound from my script. However, if I exit the script and manually run the .sh to start things the same way, my sound works perfectly.
NOT using a USB device, just the 3.5mm out built in to the raspberry. Near as I can figure maybe this is launching and prepping sounds before alsa sound is fully loaded in? But I would think I would get errors from my Python script if that was the case.
It is likely something I am overlooking. Any tips as to what that may be would be appreciated.