Tumerboy
Posts: 41
Joined: Thu May 09, 2013 4:44 am

Running a Python Script AFTER X GUI Starts up?

Sun May 12, 2013 6:22 am

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

ghans
Posts: 7756
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Running a Python Script AFTER X GUI Starts up?

Sun May 12, 2013 7:01 am

Start LXTerminal (from the "Start menu").

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

Tumerboy
Posts: 41
Joined: Thu May 09, 2013 4:44 am

Re: Running a Python Script AFTER X GUI Starts up?

Sun May 12, 2013 5:20 pm

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.

User avatar
lmarmisa
Posts: 1164
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: Running a Python Script AFTER X GUI Starts up?

Mon May 13, 2013 7:50 am

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

Tumerboy
Posts: 41
Joined: Thu May 09, 2013 4:44 am

Re: Running a Python Script AFTER X GUI Starts up?

Fri May 24, 2013 2:55 am

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.

JeremyF
Posts: 515
Joined: Wed Jan 25, 2012 9:06 pm
Contact: Website

Re: Running a Python Script AFTER X GUI Starts up?

Fri May 24, 2013 3:24 am

Tumerboy 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.
shouldn't that be

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

crazyideas
Posts: 12
Joined: Wed Aug 29, 2012 12:42 pm

Re: Running a Python Script AFTER X GUI Starts up?

Thu Aug 08, 2013 6:32 pm

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??!

Bfmersion
Posts: 6
Joined: Wed Aug 07, 2013 7:35 pm

Re: Running a Python Script AFTER X GUI Starts up?

Sun Aug 11, 2013 4:16 pm

I had similar experience but added sudo in front and it ran OK. Mine is a radio/mp3 app.

mrjoewood
Posts: 1
Joined: Sun Sep 01, 2013 3:37 pm

Re: Running a Python Script AFTER X GUI Starts up?

Sun Sep 01, 2013 3:40 pm

@crazyideas: Are you missing a slash in front of 'usr/bin'?

ashwin8
Posts: 2
Joined: Tue Nov 19, 2013 9:56 am

Re: Running a Python Script AFTER X GUI Starts up?

Wed Nov 20, 2013 6:02 am

working code:
sudo nano /etc/xdg/lxsession/LXDE/autostart

edit :add last line
@/usr/bin/python /home/pi/script.py

bluedevil678
Posts: 15
Joined: Wed Jan 09, 2013 6:36 am

Re: Running a Python Script AFTER X GUI Starts up?

Wed Feb 26, 2014 3:04 pm

GIVE THIS MAN A BELLS!!!

User avatar
picrypt
Posts: 8
Joined: Wed Nov 06, 2013 4:33 pm

Re: Running a Python Script AFTER X GUI Starts up?

Wed Mar 12, 2014 5:01 pm

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
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.

WDino
Posts: 3
Joined: Sun Oct 13, 2013 4:18 pm

Re: Running a Python Script AFTER X GUI Starts up?

Sat May 10, 2014 10:18 pm

THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

gordon77
Posts: 3243
Joined: Sun Aug 05, 2012 3:12 pm

Re: Running a Python Script AFTER X GUI Starts up?

Sun Mar 08, 2015 9:26 am

ashwin8 wrote:working code:
sudo nano /etc/xdg/lxsession/LXDE/autostart

edit :add last line
@/usr/bin/python /home/pi/script.py
I've tried this by adding @/usr/bin/python /home/pi/script.py to the autostart file but nothing happens

Anything else I need to do ?

Gordon77

gordon77
Posts: 3243
Joined: Sun Aug 05, 2012 3:12 pm

Re: Running a Python Script AFTER X GUI Starts up?

Sun Mar 08, 2015 10:26 am

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

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)
Gordon77

AbelR
Posts: 1
Joined: Wed Apr 01, 2015 12:56 pm

Re: Running a Python Script AFTER X GUI Starts up?

Wed Apr 01, 2015 1:10 pm

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:

Code: Select all

@lxterminal -e /path/to/your/script.sh
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:

Code: Select all

#!/bin/bash
[ -z "`pgrep $0`" ] && exit
# my code below
:
:
I hope this helps somebody else.

Peace \m/

mbruck
Posts: 7
Joined: Fri Jul 31, 2015 10:00 pm

Re: Running a Python Script AFTER X GUI Starts up?

Tue Sep 01, 2015 2:58 pm

This is very helpful !

Thank you!

MarkDH102
Posts: 265
Joined: Fri Feb 13, 2015 3:18 pm

Re: Running a Python Script AFTER X GUI Starts up?

Fri Feb 05, 2016 2:12 pm

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 :)

gordon77
Posts: 3243
Joined: Sun Aug 05, 2012 3:12 pm

Re: Running a Python Script AFTER X GUI Starts up?

Fri Feb 05, 2016 3:04 pm

If using Jessie use ~/.config/lxsession/LXDE-pi/autostart

MarkDH102
Posts: 265
Joined: Fri Feb 13, 2015 3:18 pm

Re: Running a Python Script AFTER X GUI Starts up?

Sat Feb 06, 2016 12:06 pm

Many thanks - worked a treat.

nlandau
Posts: 9
Joined: Sat Jul 02, 2016 9:02 am

Re: Running a Python Script AFTER X GUI Starts up?

Wed Jul 20, 2016 7:09 pm

Editing the file ~/.config/lxsession/LXDE-pi/autostart did the trick however I can't run the script as sudo.
When I run

Code: Select all

@gnome-terminal -x bash -c "/home/pi/test"
It runs great.

But-

Code: Select all

@gnome-terminal -x bash -c "sudo /home/pi/test"
Or

Code: Select all

@gnome-terminal -x bash -c "/usr/bin/sudo /home/pi/test"
Don't work.

Any ideas?

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Running a Python Script AFTER X GUI Starts up?

Wed Jul 20, 2016 7:36 pm

nlandau wrote:But-

Code: Select all

@gnome-terminal -x bash -c "sudo /home/pi/test"
Or

Code: Select all

@gnome-terminal -x bash -c "/usr/bin/sudo /home/pi/test"
Don't work.
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
Posts: 9
Joined: Sat Jul 02, 2016 9:02 am

Re: Running a Python Script AFTER X GUI Starts up?

Thu Jul 21, 2016 6:53 am

found my answer here:
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)

d4v3
Posts: 1
Joined: Fri Aug 12, 2016 10:49 am

Re: Running a Python Script AFTER X GUI Starts up?

Fri Aug 12, 2016 11:05 am

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...

jessebfox
Posts: 1
Joined: Tue Aug 23, 2016 5:58 pm

Re: Running a Python Script AFTER X GUI Starts up?

Tue Aug 23, 2016 8:03 pm

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.

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 5 guests