reltek
Posts: 20
Joined: Wed Jan 13, 2016 3:15 pm

How to autostart a program on Raspbian?

Thu Jan 14, 2016 5:28 pm

I want to autostart a program on Raspbian and having difficulty accomplishing that. How does one go about doing that.

TheRadziu
Posts: 18
Joined: Thu Jan 14, 2016 3:08 pm
Location: Poland

Re: How to autostart a program on Raspbian?

Thu Jan 14, 2016 5:46 pm

there are at least 3 ways to do so:
- by editing /etc/rc.local in my case the line I added looks like this:

Code: Select all

su - pi -c kodi
for autostarting kodi
- by creating new init.d file, here is some info about it http://unix.stackexchange.com/questions ... rt-at-boot
- by creating .desktop file somewhere but honestly I do not remember how to do it, try searching .desktop file autostart

I am using all the listed ways for different programs.

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

Re: How to autostart a program on Raspbian?

Thu Jan 14, 2016 6:36 pm

Try putting the relevant command in ~/.config/lxsession/LXDE-pi/autostart

Eg. To run a python script use...
@sudo /usr/bin/python /home/pi/filename.py

To Make the file executable with sudo chmod +x filename.py

It will run after the gui starts.
Last edited by gordon77 on Wed Apr 27, 2016 6:34 am, edited 1 time in total.

reltek
Posts: 20
Joined: Wed Jan 13, 2016 3:15 pm

Re: How to autostart a program on Raspbian?

Thu Jan 14, 2016 7:44 pm

I was able to edit rc.local for autostarting my program as follows by simply adding a line.

Code: Select all

./home/pi/myprogram/viewdata
That worked. Viewdata started up at boot, but Viewdata doesn't know it's working folder. ViewData complains that it can't access it properties or setting, because working folder is where they are located and Viewdata program also in the same folder. So, how do you let my program viewdata know what is its working folder?

TrianP
Posts: 7
Joined: Sat Jan 02, 2016 8:19 pm

Re: How to autostart a program on Raspbian?

Wed Feb 17, 2016 3:02 am

Adding the entry to the autostart file worked for me -- except I run into a problem with the sudo password. If I've executed a command with sudo rights before I start the desktop, my application starts up just fine. However, if I start the desktop after bootup without running a sudo command, the system hangs with a blank screen. I'm sure it's because the system is looking for the user password.

Can you insert the password into the autostart file? Is there another way to avoid this problem?

Thanks!

User avatar
brekee12
Posts: 335
Joined: Wed Feb 03, 2016 3:36 pm
Location: HU

Re: How to autostart a program on Raspbian?

Wed Feb 17, 2016 7:57 am

For these problems:
- make sure user rigths is set correctly. Are you sure your program needs root privileges? Could not you run with another user?
- your python program should contain the full path for files or the PATH with your needs should be set before your program runs
Brekee12
on a Raspberry B+ with whezzy, two Zero with Jessie Light

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: How to autostart a program on Raspbian?

Wed Feb 17, 2016 9:01 am

There are ways around the program not starting with the correct directory current. For example you could write a new program that sets the working directory and then starts the program you want.

But the correct way to do it is for the program to have a confguration file with the directory specified in it.

User avatar
RaTTuS
Posts: 10539
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: How to autostart a program on Raspbian?

Wed Feb 17, 2016 9:15 am

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

TrianP
Posts: 7
Joined: Sat Jan 02, 2016 8:19 pm

Re: How to autostart a program on Raspbian?

Wed Feb 17, 2016 1:48 pm

I think the program is starting in the right directory - I've specified it completely, and if I've run a command using sudo rights that stay in effect, startx loads correctly and the application starts. Therefore, I think the problem is with the need for a password.

I think Brekee12 is on the right track with sudo rights. My program accesses the GPIO ports, and as far as I can tell, it only runs with sudo rights. I always get permission errors if I try to run it without sudo rights.

It might be as simple as giving the account permanent sudo rights. I'm very much a Linux newbie, so I'm a bit in the dark. Is there an easy way to do this? Is the default pi account a sudo account? I could try the program there if that's the case.

Thanks for the replies.

User avatar
brekee12
Posts: 335
Joined: Wed Feb 03, 2016 3:36 pm
Location: HU

Re: How to autostart a program on Raspbian?

Wed Feb 17, 2016 2:39 pm

I can't remember what topic I have read that GPIOs needs sudo rigths. I use PIGPIO library programming the ios and only the pigpiod needs sudo right to start. To be honest I have put it into init.d and any time anyway I can use the GPIOs. Maybe, it makes sense to see joan's PIGPIO library which is much more than just io subrutins and does not requires sudorigth for the user.
Brekee12
on a Raspberry B+ with whezzy, two Zero with Jessie Light

TrianP
Posts: 7
Joined: Sat Jan 02, 2016 8:19 pm

Re: How to autostart a program on Raspbian?

Fri Feb 19, 2016 5:17 pm

Thanks for the reply - I'll try that library this weekend.

However, the problem will likely remain. I'm using some one-wire sensors and a magnetometer. It's likely that at least one of the libraries for those devices will need sudo rights. Hopefully not!

But it's still an interesting question: is there a way to insert a python script that needs sudo rights into the autostart file? I'd like to know for future projects even if not for this one.

Thanks!

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

Re: How to autostart a program on Raspbian?

Fri Feb 19, 2016 6:14 pm

TrianP wrote: But it's still an interesting question: is there a way to insert a python script that needs sudo rights into the autostart file? I'd like to know for future projects even if not for this one.

Thanks!
Just precede it with sudo

Code: Select all

@sudo python /path/to/script/something.py
That shouldn't ask for a password unless you've changed the sudoers file to require a password when sudo is used.

EDIT: added last line

TrianP
Posts: 7
Joined: Sat Jan 02, 2016 8:19 pm

Re: How to autostart a program on Raspbian?

Fri Feb 19, 2016 10:36 pm

Success! The reply about the sudoers file led me to look at that file. I saw that the pi account was set to not ask for a password (pi ALL=(ALL) NOPASSWD:ALL). However, I've been working in a different account. I added a matching line to the file for my account using the visudo command, and the system no longer asks for a password. Consequently, the application starts up when the desktop starts.

Thanks very much JimmyN and everyone else for your help!

reltek
Posts: 20
Joined: Wed Jan 13, 2016 3:15 pm

Re: How to autostart a program on Raspbian?

Tue May 10, 2016 7:27 pm

Sadly none of the suggested answers helped. The only way I was able to get my program to run at boot with correct work path folder is writing a mini program, which runs at boot, started myprogram.

User avatar
RaTTuS
Posts: 10539
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: How to autostart a program on Raspbian?

Wed May 11, 2016 8:51 am

reltek wrote:Sadly none of the suggested answers helped. The only way I was able to get my program to run at boot with correct work path folder is writing a mini program, which runs at boot, started myprogram.
probably because you have done it worng
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

aporeg
Posts: 2
Joined: Sat Oct 10, 2015 11:58 am

Re: How to autostart a program on Raspbian?

Fri Jun 16, 2017 2:18 pm

First of all Hello to all

Thanks @TheRadziu

This worked for me

Code: Select all

su - pi -c kodi
This is how is should look like after edit

Code: Select all


# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
su - pi -c kodi

exit 0

pvklink
Posts: 15
Joined: Wed Mar 08, 2017 3:03 pm

Re: How to autostart a program on Raspbian?

Wed Dec 20, 2017 7:25 pm

Hi my autostart (raspbian stretch + pico menu) still does executing the menu instead of my webbrowser

i tried sudo nano /etc/xdg/lxsession/LXDE/autostart
and i tried sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
I commented out all the old existing lines with #

my file is:

# start boot default
#@lxpanel --profile LXDE-pi
#@pcmanfm --desktop --profile LXDE-pi
#@xscreensaver -no-splash
#@point-rpi
# end boot default

# Auto run the browser
@xset s off
@xset -dpms
@xset s noblank
@apropos chromium
@chromium-browser --start-fullscreen http://www.nu.nl
#end pvk
Last edited by pvklink on Sun Dec 24, 2017 12:36 pm, edited 1 time in total.

Trash_Can_Man
Posts: 24
Joined: Wed Dec 20, 2017 6:49 pm

Re: How to autostart a program on Raspbian?

Sat Dec 23, 2017 5:42 pm

There are several lxde autostart files on different places, the one that works for me on the "oficial" raspbian with pixel is:

Code: Select all

/home/pi/.config/lxsession/LXDE-pi/autostart
edit it with:

Code: Select all

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
this totally works for me, i used it for a while starting kodi on desktop start...

raspisnagger
Posts: 1
Joined: Wed Jan 24, 2018 2:37 pm

Re: How to autostart a program on Raspbian?

Wed Jan 24, 2018 2:52 pm

I wrote "@sudo /usr/bin/python /home/pi/Desktop/Filename.py" but on startup nothing happens. Where did I go wrong? We know the path works when executed through the console.

klricks
Posts: 6852
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: How to autostart a program on Raspbian?

Wed Jan 24, 2018 6:11 pm

raspisnagger wrote:
Wed Jan 24, 2018 2:52 pm
I wrote "@sudo /usr/bin/python /home/pi/Desktop/Filename.py" but on startup nothing happens. Where did I go wrong? We know the path works when executed through the console.
Try:

Code: Select all

@lxterminal -e python /home/pi/Desktop/Filename.py
also since autostart is in home/pi you should not use sudo to edit the file.

Code: Select all

nano /home/pi/.config/lxsession/LXDE-pi/autostart
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

viktor6
Posts: 24
Joined: Sun Aug 27, 2017 7:10 pm

Re: How to autostart a program on Raspbian?

Sun Feb 11, 2018 9:44 pm

aporeg wrote:
Fri Jun 16, 2017 2:18 pm
First of all Hello to all

Thanks @TheRadziu

This worked for me

Code: Select all

su - pi -c kodi
This is how is should look like after edit

Code: Select all


# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi
su - pi -c kodi

exit 0
There is no restart in the menu, and the rest

Zakhar
Posts: 1
Joined: Sat Oct 19, 2019 12:04 pm

Re: How to autostart a program on Raspbian?

Sat Oct 19, 2019 12:46 pm

So here is how it works!

First, in the Linux spirit, your question is incomplete. Bear in mind Linux has always been made for servers and is multi-users by default, unlike another more widespread proprietary O.S.

So the question is, do you want to "autostart" something for:
  • the whole machine (commonly called "a service")
  • only for the current user in session
For the former, ie starting a service, you would add a service to systemd or something in rc.local.
Your service will be started as root, because you are in a phase where there is not yet any user logged in, and the service would be accessible to whatever user logs in the machine later.
Also, remember that at this point you might NOT have a graphical environment, so no graphical program should be put here.
A typical use case would be starting a web proxy (like Squid) that all users would use for their browsing sessions.

For the later, you want the session to be started, and you want the program to be launched with the user account, and not with root, otherwise there is a misunderstanding of basic security!
So here, you use the LXDE autostart described above.
Programs here are launched with the account on the user, and within the desktop environment that is started. So a graphical program is suitable here.

But there is a nasty trick!
If you read carefully the documentation, it says that if there is a user's local autostart, this one is executed instead of the "global" one.
This means that you cannot just create an autostart and put your command in it, because then only your command would run and not what was in the "global" autostart that is responsible for showing your desktop.
As a result you would end up with a black screen with a mouse pointer and you program running.

So the correct way to do things is:

First, copy the "global" autostart to your config directory

Code: Select all

cp /etc/xdg/lxsession/LXDE-pi/autostart ~/.config/lxsession/LXDE-pi
Then add your command after the standard ones, which in my example reads like that:

Code: Select all

cat ~/.config/lxsession/LXDE-pi/autostart
@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
my_program
That way, you still have your desktop showing, plus "my_program" will run once.


Note on Kodi
If the program you want to run is kodi, know that kodi has 2 modes.
The standard mode of Kodi (aka "standalone") is that it does NOT need a DE (Desktop Environment), but only a basic graphic stack (I don't know exactly but things like Mesa/X11).
The other mode is known as "full screen" and is made to run on top of a graphical environement, like Openbox/LXDE in the case of classic Raspbian.

So if you intended to run Kodi "standalone" you are very mistaken using Raspbian, because the DE in this case is useless and a nuisance to Kodi. If this is your intention, you'd better use something like LibreElec which is exactly that: minimal stuff to run Kodi.

So if you intend to run Kodi on Raspbian, and want it autostart, the right command is

Code: Select all

kodi -fs
That will run Kodi "full-screen" on top of LXDE. Should you decide that you want to exit Kodi and do usual desktop stuff, you just need to click on "exit" and you are back to LXDE... which does not work on "standalone" mode!

Return to “Raspbian”