Page 1 of 1

how to auto run a program when pi start?

Posted: Wed May 11, 2016 5:49 pm
by yaaang3000
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?

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

Posted: Thu May 12, 2016 9:23 am
by topguy
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

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

Posted: Thu May 12, 2016 10:26 am
by MarkHaysHarris777
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.

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

Posted: Thu May 12, 2016 10:47 am
by Pilosopong Tasyo
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.

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

Posted: Thu May 12, 2016 10:51 am
by RaTTuS
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

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

Posted: Thu May 12, 2016 11:04 am
by Pilosopong Tasyo
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"

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

Posted: Thu May 12, 2016 11:27 am
by yaaang3000
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?

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

Posted: Thu May 12, 2016 11:46 am
by shuckle
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?

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

Posted: Thu May 12, 2016 11:48 am
by kusti8
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.

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

Posted: Thu May 12, 2016 11:50 am
by RaTTuS
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.... ;)

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

Posted: Thu May 12, 2016 12:19 pm
by Pilosopong Tasyo
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...

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

Posted: Thu May 12, 2016 12:24 pm
by yaaang3000
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:

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

Posted: Thu May 12, 2016 12:46 pm
by Pilosopong Tasyo
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"

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

Posted: Thu May 12, 2016 1:14 pm
by yaaang3000
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:

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

Posted: Thu May 12, 2016 1:22 pm
by Pilosopong Tasyo
yaaang3000 wrote:omg it works!!!!! thank you soooooooooooooooo much!!! :lol: :lol:
Cool beans! You're welcome. :D

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

Posted: Thu May 12, 2016 3:20 pm
by MarkHaysHarris777
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.

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

Posted: Thu May 12, 2016 4:58 pm
by kolban
Another thought on starting programs at boot time is to create a "systemd" service ... see this video:

https://www.youtube.com/watch?v=eEuViHanjKI

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

Posted: Thu May 12, 2016 5:02 pm
by stderr
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.

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

Posted: Thu May 12, 2016 5:10 pm
by stderr
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

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

Posted: Fri May 13, 2016 4:15 am
by Pilosopong Tasyo
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.