yaaang3000
Posts: 15
Joined: Wed May 11, 2016 5:37 pm

how to auto run a program when pi start?

Wed May 11, 2016 5:49 pm

hi,i want to run a program automatically when the pi is start.This program include some GUI part,normally i just use "sudo python gg.py" to run it.I tried to put some code into /etc/rc.local,but it didn't work,but some other programs,which just use to control some LEDs,are working well.
so...how to solve this problem? i mean,how to make the program to show its window automatically when the pi is started?

User avatar
topguy
Posts: 5759
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: how to auto run a program when pi start?

Thu May 12, 2016 9:23 am

When you say "GUI" I assume X-windows gui ?

There are so many threads here about autostarting applications on the desktop: search.php?keywords=lxde+autostart

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: how to auto run a program when pi start?

Thu May 12, 2016 10:26 am

The typical way to auto-start stuff is by placing a driver script in /etc/rc.local, like this:

Code: Select all

su - pi -c "/home/pi/bin/driver.sh &"
The command says, run as user (pi) the command (-c) driver.sh located in (/home/pi/bin) in the background (&).

Then you place whateveryouwant in the driver.sh script, in your personal bin directory.

Sometimes things don't work right because of permissions, sometimes its timing, other times its because "you" spelled something wrong. Often things don't work because of a careless mistake... always double check your work; corollary, always suspect your own work first.
marcus
:ugeek:

User avatar
Pilosopong Tasyo
Posts: 116
Joined: Wed Apr 06, 2016 11:43 am
Location: /root

Re: how to auto run a program when pi start?

Thu May 12, 2016 10:47 am

yaaang3000 wrote:...run a program automatically when the pi is start. This program include some GUI part,... I tried to put some code into /etc/rc.local, but it didn't work, but some other programs, which just use to control some LEDs,are working well.
Never use rc.local to run GUI apps. rc.local runs *before* the X-server gets loaded. A better way is to edit the ~/.config/lxsession/LXDE-pi/autostart file and add your @program at the end.

In the following example:

Code: Select all

pi@rpi3:~ $ cat ~/.config/lxsession/LXDE-pi/autostart 
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@chromium-browser
chromium-browser runs once the Pi boots and I'm logged into the desktop.
Give a man a ghoti and he'll eat for a day; teach him how to ghoti and he'll eat for life. Ghoti?! - http://i.imgur.com/mAfBUdi.png
How To Ask Questions The Smart Way - http://www.catb.org/esr/faqs/smart-questions.html

User avatar
RaTTuS
Posts: 10456
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: how to auto run a program when pi start?

Thu May 12, 2016 10:51 am

Pilosopong Tasyo wrote:
yaaang3000 wrote:...run a program automatically when the pi is start. This program include some GUI part,... I tried to put some code into /etc/rc.local, but it didn't work, but some other programs, which just use to control some LEDs,are working well.
Never use rc.local to run GUI apps. rc.local runs *before* the X-server gets loaded. A better way is to edit the ~/.config/lxsession/LXDE-pi/autostart file and add your @program at the end.

In the following example:

Code: Select all

pi@rpi3:~ $ cat ~/.config/lxsession/LXDE-pi/autostart 
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@chromium-browser
chromium-browser runs once the Pi boots and I'm logged into the desktop.
also don't use rc.local if you
a) want to interact with it
b) want it to display anything
c) don't want to lock up your machine unless it exits or you & it
YMMV
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
Pilosopong Tasyo
Posts: 116
Joined: Wed Apr 06, 2016 11:43 am
Location: /root

Re: how to auto run a program when pi start?

Thu May 12, 2016 11:04 am

RaTTuS wrote:also don't use rc.local if you
a) want to interact with it
b) want it to display anything
c) don't want to lock up your machine unless it exits or you & it
Yes, those as well :)

And to add, you may have to specify full paths to the program you intend to run, just to be on the safe side:

Code: Select all

pi@rpi3:~ $ which sudo
/usr/bin/sudo

pi@rpi3:~ $ which python
/usr/bin/python

Code: Select all

@/usr/bin/sudo /usr/bin/python "/location/of the/program called/gg.py"
Of you just might get away with

Code: Select all

@sudo python "/location/of the/program called/gg.py"
Last edited by Pilosopong Tasyo on Thu May 12, 2016 12:26 pm, edited 1 time in total.
Give a man a ghoti and he'll eat for a day; teach him how to ghoti and he'll eat for life. Ghoti?! - http://i.imgur.com/mAfBUdi.png
How To Ask Questions The Smart Way - http://www.catb.org/esr/faqs/smart-questions.html

yaaang3000
Posts: 15
Joined: Wed May 11, 2016 5:37 pm

Re: how to auto run a program when pi start?

Thu May 12, 2016 11:27 am

Pilosopong Tasyo wrote:
yaaang3000 wrote:...run a program automatically when the pi is start. This program include some GUI part,... I tried to put some code into /etc/rc.local, but it didn't work, but some other programs, which just use to control some LEDs,are working well.
Never use rc.local to run GUI apps. rc.local runs *before* the X-server gets loaded. A better way is to edit the ~/.config/lxsession/LXDE-pi/autostart file and add your @program at the end.

In the following example:

Code: Select all

pi@rpi3:~ $ cat ~/.config/lxsession/LXDE-pi/autostart 
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@chromium-browser
chromium-browser runs once the Pi boots and I'm logged into the desktop.


thanks for you answer,but my program is just a gg.py document,how to use "@..." with .py document?

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: how to auto run a program when pi start?

Thu May 12, 2016 11:46 am

Why b) want it to display anything ?

I am starting several omxplayers from rc.local and they do display videos. How should that then be done if not from rc.local?

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: how to auto run a program when pi start?

Thu May 12, 2016 11:48 am

shuckle wrote:Why b) want it to display anything ?

I am starting several omxplayers from rc.local and they do display videos. How should that then be done if not from rc.local?
Omxplayer doesn't use X so it has no need to start when X starts and still works. It could alternatively be done with systemd or cron. For GUI applications, the above way, not rc.local, is the best way.
There are 10 types of people: those who understand binary and those who don't.

User avatar
RaTTuS
Posts: 10456
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: how to auto run a program when pi start?

Thu May 12, 2016 11:50 am

shuckle wrote:Why b) want it to display anything ?

I am starting several omxplayers from rc.local and they do display videos. How should that then be done if not from rc.local?
because, in general , rc.local does not have a terminal attached [YMMV depending], so nothing will be displayed ,
omxplayer uses direct access to the framebuffer so it will work [again YMMV]
but you cannot interact with them via the keyboard.... ;)
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
Pilosopong Tasyo
Posts: 116
Joined: Wed Apr 06, 2016 11:43 am
Location: /root

Re: how to auto run a program when pi start?

Thu May 12, 2016 12:19 pm

yaaang3000 wrote:...but my program is just a gg.py document,how to use "@..." with .py document?
The "@" is just a convention used by LXDE (which Raspbian uses as it's desktop environment). If you notice from the other entries in the autostart file, all of them have an @ at the beginning. So, when in Rome...
Give a man a ghoti and he'll eat for a day; teach him how to ghoti and he'll eat for life. Ghoti?! - http://i.imgur.com/mAfBUdi.png
How To Ask Questions The Smart Way - http://www.catb.org/esr/faqs/smart-questions.html

yaaang3000
Posts: 15
Joined: Wed May 11, 2016 5:37 pm

Re: how to auto run a program when pi start?

Thu May 12, 2016 12:24 pm

Pilosopong Tasyo wrote:
yaaang3000 wrote:...but my program is just a gg.py document,how to use "@..." with .py document?
The "@" is just a convention used by LXDE (which Raspbian uses as it's desktop environment). If you notice from the other entries in the autostart file, all of them have an @ at the beginning. So, when in Rome...


so emm..should it be @gg.py ? or something else? i'm still confusing... :cry:

User avatar
Pilosopong Tasyo
Posts: 116
Joined: Wed Apr 06, 2016 11:43 am
Location: /root

Re: how to auto run a program when pi start?

Thu May 12, 2016 12:46 pm

Sorry, I forgot to include the @ in an earlier post. I edited it to reflect the change. It should be one of the following:

Code: Select all

@/usr/bin/sudo /usr/bin/python "/location/of the/program called/gg.py"

Code: Select all

@sudo python "/location/of the/program called/gg.py"
Give a man a ghoti and he'll eat for a day; teach him how to ghoti and he'll eat for life. Ghoti?! - http://i.imgur.com/mAfBUdi.png
How To Ask Questions The Smart Way - http://www.catb.org/esr/faqs/smart-questions.html

yaaang3000
Posts: 15
Joined: Wed May 11, 2016 5:37 pm

Re: how to auto run a program when pi start?

Thu May 12, 2016 1:14 pm

Pilosopong Tasyo wrote:Sorry, I forgot to include the @ in an earlier post. I edited it to reflect the change. It should be one of the following:

Code: Select all

@/usr/bin/sudo /usr/bin/python "/location/of the/program called/gg.py"

Code: Select all

@sudo python "/location/of the/program called/gg.py"


omg it works!!!!! thank you soooooooooooooooo much!!! :lol: :lol:

User avatar
Pilosopong Tasyo
Posts: 116
Joined: Wed Apr 06, 2016 11:43 am
Location: /root

Re: how to auto run a program when pi start?

Thu May 12, 2016 1:22 pm

yaaang3000 wrote:omg it works!!!!! thank you soooooooooooooooo much!!! :lol: :lol:
Cool beans! You're welcome. :D
Give a man a ghoti and he'll eat for a day; teach him how to ghoti and he'll eat for life. Ghoti?! - http://i.imgur.com/mAfBUdi.png
How To Ask Questions The Smart Way - http://www.catb.org/esr/faqs/smart-questions.html

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: how to auto run a program when pi start?

Thu May 12, 2016 3:20 pm

Pilosopong Tasyo wrote:
yaaang3000 wrote:...run a program automatically when the pi is start. This program include some GUI part,... I tried to put some code into /etc/rc.local, but it didn't work, but some other programs, which just use to control some LEDs,are working well.
Never use rc.local to run GUI apps. rc.local runs *before* the X-server gets loaded. A better way is to edit the ~/.config/lxsession/LXDE-pi/autostart file and add your @program at the end.
Nonsense!

... my /etc/rc.local startup script (Vnc2.sh) starts my X server ! ... and a lot of other stuff, some of which is graphical display, some of which is timer dependent, and all of which is multi-user /

I actually have four X servers running on my PI stations; shared. Each of those X servers is started at boot-up vi /etc/rc.local, and then each runs special additional programs from each of their respective user ~/bin/ folders after the Vnc X server is running...

... been doing that for many years on various systems; have adapted an old technique for my PIs.
marcus
:ugeek:

User avatar
kolban
Posts: 143
Joined: Fri Dec 04, 2015 1:45 am
Location: Texas, USA

Re: how to auto run a program when pi start?

Thu May 12, 2016 4:58 pm

Another thought on starting programs at boot time is to create a "systemd" service ... see this video:

https://www.youtube.com/watch?v=eEuViHanjKI
FREE book on Raspberry Pi usage and programming

https://leanpub.com/pi

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

Re: how to auto run a program when pi start?

Thu May 12, 2016 5:02 pm

MarkHaysHarris777 wrote:The typical way to auto-start stuff is by placing a driver script in /etc/rc.local, like this:

Sometimes things don't work right because of permissions, sometimes its timing, other times its because "you" spelled something wrong. Often things don't work because of a careless mistake...
Also, rc.local is run the -e option so it will bomb out at the slightest sneeze of error.

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

Re: how to auto run a program when pi start?

Thu May 12, 2016 5:10 pm

yaaang3000 wrote:
Pilosopong Tasyo wrote:The "@" is just a convention used by LXDE (which Raspbian uses as it's desktop environment). If you notice from the other entries in the autostart file, all of them have an @ at the beginning. So, when in Rome...
so emm..should it be @gg.py ? or something else? i'm still confusing... :cry:
Actually, it has meaning, so you should use it or not depending on what you actually want to occur:

http://wiki.lxde.org/en/LXDE:Questions# ... _launch.3F

How do I make an application autostart on LXDE launch?

You just add a .desktop file to ~/.config/autostart or add the commands to .config/lxsession/LXDE/autostart to have a program start for a specific user. Less commonly, for system wide startup, you can edit the file /etc/xdg/lxsession/LXDE/autostart.
Each line in autostart file means the command of application to autostart. If you want any application to be reload when it is terminated abnormally, add @ sign at the beginning of the line.
autostart file example:

@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
@xscreensaver -no-splash
@ibus-daemon -xrd
@tomboy --panel-applet

User avatar
Pilosopong Tasyo
Posts: 116
Joined: Wed Apr 06, 2016 11:43 am
Location: /root

Re: how to auto run a program when pi start?

Fri May 13, 2016 4:15 am

MarkHaysHarris777 wrote:Nonsense!

<snipped>... been doing that for many years on various systems...
Considering the support question was posted in the Beginners sub-forum, the OP may or may not appreciate your advanced old-school insight; the OP may or may not even care -- for now -- the technicalities of whether my "nonsense" advice is absolute and perfectly correct *in all cases,* and I don't claim to be a know-it-all. The OP only needs to know what is relevant, within context, and pragmatically applicable to the problem at hand. (S)He will cross that technical bridge when the opportunity presents itself.

All of this may be old hat to you, but everyone were newbies at some point, and we learn something new every day. That includes you.
stderr wrote:Actually, it has meaning, so you should use it or not depending on what you actually want to occur:
...
If you want any application to be reload when it is terminated abnormally, add @ sign at the beginning of the line.
Thanks. I learned something new about LXDE today. I been used to the Unity DE on my other desktop. Different way to skin a cat.
Give a man a ghoti and he'll eat for a day; teach him how to ghoti and he'll eat for life. Ghoti?! - http://i.imgur.com/mAfBUdi.png
How To Ask Questions The Smart Way - http://www.catb.org/esr/faqs/smart-questions.html

Return to “Beginners”