mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Autostart py script

Tue Apr 26, 2016 6:42 pm

I've edited the autostart folder in /etc/xdg/lxsession/LXDE-pi/autostart

to include --

@/usr/bin/python/home/pi/myprogram.py

and nothing.

Can't get this to work -- trying to autostart the application. Raspian opens to the CLI, not the desktop. (and tried both for heck of it) What am I doing wrong?

-- thank you.
Last edited by mmkw43 on Tue Apr 26, 2016 7:46 pm, edited 1 time in total.

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

Re: Autostart py script

Tue Apr 26, 2016 6:54 pm

mmkw43 wrote:I've edited the autostart folder in /etc/xdg/lxsession/LXDE-pi/autostart
When does it use that? I'm starting things in the specific user's autostart which is in ~/.config/lxsession/LXDE at least in x86 Ubuntu 16.04.
to include --
@/usr/bin/python/home/pi/myprogram.py
and nothing.
Can't get this to work -- trying to autostart the application. Raspian opens to the CLI, not the desktop. What am I doing wrong?
I'm only starting normal executables so does the feature work with scripts directly like that? Is your script executable? Does it have the correct shebang? Did you try putting python3 and then the script name? Do other programs start when you put them in there, e.g. terminal or something ssh -X ?

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Tue Apr 26, 2016 7:44 pm

yes -- shebang is there program works fine whether I start it in terminal or desktop.

the instructions I found were online so that may be a problem. can't get it to work at all. that IS the path to the autostart folder, I even see the xscreensaver in there. and yeas, put the py script last.


mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Tue Apr 26, 2016 9:09 pm

ok thanks.

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 12:05 am

fyi -- I still can't get it to work. everything is right, my py file is executable. tried both of the autostart folders - (/etc/xdg/ etc and .config)

but nothing.
guess I'll try that rc.local thing ? that must be the preferred method?

because this simply doesn't work.

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 12:52 am

spent half the day on this -- from what I've read cron is the easy way to do it -- i'll try it in the morning. way too much content out there on something so simple. every post has a half dozen techniques on how to auto run a script. :roll:

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

Re: Autostart py script

Wed Apr 27, 2016 7:33 am

mmkw43 wrote:fyi -- I still can't get it to work. everything is right, my py file is executable. tried both of the autostart folders - (/etc/xdg/ etc and .config)

but nothing.
guess I'll try that rc.local thing ? that must be the preferred method?

because this simply doesn't work.

It works for me. Note it's pi not PI.

stubright
Posts: 109
Joined: Sat Dec 24, 2011 11:12 pm

Re: Autostart py script

Wed Apr 27, 2016 10:02 am

mmkw43 wrote:@/usr/bin/python/home/pi/myprogram.py
Shouldn't that be

Code: Select all

@/usr/bin/python /home/pi/myprogram.py
with a space between /python and /home.
Also check all your paths in the script are absolute and not relative.
It would help if you posted your code too.
Stu

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 3:19 pm

Thanks Stu --

Had no space there (typo in an example I read) but still can't get it going. Questions --

(1) Raspian is booting to desktop (and I tried CLI also) but that shouldn't make a difference, correct?

(2) Yes, using the absolute path /home/pi/myprogram.py (and as I said, shebang is there and program runs perfectly)

(3) Which autostart folder should I use and what is the difference? For my own education why are there 2 folders that are autostart? I've seen autostart examples that show using either one. ? ! Should the command be in both? (If so, I didn't try that).

/etc/xdg/lxsession/LXDE-pi/autostart and /home/pi/.config/lxsession/LXDE-pi/autostart

(4) Should I include sudo? Tried either way but no luck.
@(sudo) /usr/bin/python3.4 /home/pi/myprogram.py (put space between 3.4 and /home)

Should the python version number be there? That is the absolute path as written isn't it ?

I'm putting it at the end of the autostart file and yes, it saved fine and shows up.Right after xscreensaver, which starts fine.

I must be missing something very simple -- no doubt I'll be embarrassed. -- thank you

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Autostart py script

Wed Apr 27, 2016 4:19 pm

There is a global autostart file at "/etc/xdg/lxsession/LXDE-pi/autostart". That applies to all users unless they have created a users autostart at "~/.config/lxsession/LXDE-pi/autostart".

Jessie now creates a users autostart by default during installation, so many of the guides/howto's are outdated, as you now have to make your changes to the users autostart rather than the global one.

Do you have to use "sudo" to run the script from the command line? If so you'll need to use it in the autostart line, if "sudo" is not needed in the command line you won't need it in the autostart file.

Try this

Code: Select all

nano ~/.config/lxsession/LXDE-pi/autostart
Add this line at the bottom

Code: Select all

@/usr/bin/python /home/pi/myprogram.py
or if "sudo" is required then use

Code: Select all

@sudo /usr/bin/python /home/pi/myprogram.py
If that doesn't work show us what the shebang line looks like to make sure which Python version you should be using for the autostart.

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 5:00 pm

here comes my embarrassing question ?

~/ that tilde ?

I've been using /home/pi/.config/lxsession/LXDE-pi/autostart (no tilde -- is that a non pi linux thing?)



embarrassing question 2?

In file manager there are 2 main folders in the directory tree when all is closed. (Raspian)
(1) pi
(2) /

But if I go to (2) and /home it's shown as the same path as (1) /home/pi so I've always considered them the same.

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 5:02 pm

my shebang line is --

#!/usr/bin/env python3.4

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: Autostart py script

Wed Apr 27, 2016 5:48 pm

I have had the same struggles and came up with one solution that seems to work all the time. My python script runs in X using tkinter AND it also uses the GPIO. I could find any examples that would make it autostart including all the above helps.

So, I created a non GUI, non GPIO python script, made it executable and placed it in the home directory. Then, I edited the autostart folder to run this file on boot.

@/usr/bin/python /home/pi/startmyprogram.py

This script contains a system call to start the real python script: This method has been 100% reliable. Hope that helps.

Code: Select all

#!/usr/bin/env python3
#import python system modules
import time
import system
import subprocess
import os

time.sleep(1)
os.system("cd /home/pi")   #navigate to the correct path
time.sleep(1)
os.system("sudo python3.2 myprogram.py") #launch the main python program
time.sleep(10)
exit

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 6:00 pm

YES -- haven't read you reply yet but I also am using tkinter and gpio -- now I'll read what you say.

maybe good news -- thanks.

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 6:03 pm

OK -- beautiful. I bet that works and funny because I possibly wanted some sort of opening script -- like a built in time delay that goes to the app or let's you do something else -- good deal. will play with it.

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Autostart py script

Wed Apr 27, 2016 6:11 pm

The "~/" is just shorthand for the home directory of the user you're logged in as, in this case "/home/pi". So "~/Documents" is the same path as "/home/pi/Documents", but with less typing. If you were logged in as "Sleepy" then "~/Documents" would take you to Sleepy's documents, if logged in as "Grumpy" then it would take you to Grumpy's documents. Inside config files it's best to always use the full path, but when changing to the home directory or starting an editor the "~/" method works well.

"/pi" is the users home directory in "/". So when you look in "/" and scroll down to "/home/" you'll find "pi" again. The "/pi" entry is provided as a shortcut to the "home" files for "pi", so you won't have to navigate the file tree each time to get there, either will get you to the same place.

The shebang line indicates you're using Python 3.4 rather than 2.7. So the start line needs to use that rather than "python".

Code: Select all

@/usr/bin/python3 /home/pi/myprogram.py
That should work, if it doesn't show us what command line you use to start it manually. It does work, I have 5 RPI's and all of them start Python and shell scripts using the autostart.

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 6:29 pm

yes -- that's what I figured on the tilde.

Henry --
haven't done that startup code yet but at first glance I see both system and subprocess in there yet you're only using system?

can't you just use subprocess and do a subprocess.call ? or maybe your making it friendly for all python versions come to think of it.

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Wed Apr 27, 2016 7:06 pm

Henry -- I could kiss you ! (not)

Works great -- just had to change import system to sys -- eur friggin eka moment -- spent hours with this.

THANKS !

Plus I have a startup script to goof with.

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: Autostart py script

Thu Apr 28, 2016 9:18 am

Great! Sorry for the import errors, its been a while since I messed with this.

Henry

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Thu Apr 28, 2016 7:13 pm

Henry -- (or anybody)

It's one thing for me to get a problem solved but another to fully understand why there was one. Does anyone have an easy explanation for why, when you import tkinter and gpio in your code, I had this startup issue?

And what is it that the the separate script is doing to make it work?
I guess maybe it allows certain resources to load before having to deal with tkinter and gpio? Would it make any difference in my original script where I had the imports positioned or something? --thanks no worries if there isn't an answer.

henryhanselscott
Posts: 107
Joined: Sun Jan 18, 2015 1:31 pm

Re: Autostart py script

Thu Apr 28, 2016 10:00 pm

I have no idea why it works, I would also like to hear a definitive answer. My guess is a resource availability/timing issue. This was the only way I could work it out when I went down this same road about a year ago, the project that I was working on had to have this function or it was DOA. I do not know if this is the correct way to do it, but it works and has been 100% reliable.

Anybody please.......

mmkw43
Posts: 544
Joined: Tue Dec 24, 2013 6:18 pm

Re: Autostart py script

Fri Apr 29, 2016 2:17 pm

Yeah -- I've had it going since you told me about it and works great. Now however I'm possibly thinking of doing an auto run from the command line rather than the desktop, so I'm going to try and adapt it to that. anyway, thanks for the tip. I was lucky you saw the post.

cccccc
Posts: 2
Joined: Mon May 02, 2016 2:31 pm

Re: Autostart py script

Mon May 02, 2016 2:33 pm

Hi,

I have installed the raspbian using Noobs and Tried the Autostart steps given below.

nano /etc/xdg/lxsession/LXDE-pi/autostart

Added @lxterminal

then rebooted the system my system boots automatically without login in Pi user but it does not open the terminal automatically.

Then I open it manually and done the startx on terminal.
It opens the terminal automatically .

But next time when I putted a reboot command & reboot my system ,it asked me a login ID and password on GUI to login to Raspnerry pi .

I tried to login using username pi and password raspberry ,but I am not able to login to the session.

Please suggest the solution why i am not able to login to PI & also I want it to Login automatically in LXDE session without asking user and password.

I have tried it by chnaging the pi password and login with the new password still it not worked.

Also I have added a new test user and tried the same steps .
nano /etc/xdg/lxsession/LXDE-pi/autostart

Added @lxterminal

now it able to login with my new username and password .But I want to login to this session automatically & open the terminal.

I tried the rasp-config but not worked .

Please suggest solution .

Thanks.

JimmyN
Posts: 1109
Joined: Wed Mar 18, 2015 7:05 pm
Location: Virginia, USA

Re: Autostart py script

Mon May 02, 2016 3:57 pm

Raspi-config should work.

Code: Select all

sudo raspi-config
Choose Option #3 "Boot Options".
Choose Option #B4 "Desktop Autologin"

When you reboot it should boot to the desktop and you'll be logged in. Then you can use the autostart file, which only works when the desktop starts up.

The edits are no longer done in "/etc/xdg/lxsession/LXDE-pi/autostart", that is the global autostart. Now you need to make the changes in the users autostart at "~/.config/lxsession/LXDE-pi/autostart". So edit the file with

Code: Select all

nano ~/.config/lxsession/LXDE-pi/autostart

Return to “General discussion”