Page 1 of 1

Running a program automatically as the Raspberry pi boots up

Posted: Tue Dec 31, 2013 11:45 am
by VISWAKUMARAN
I just programmed the raspberry pi to capture & save images at certain regular interval. I need to know whether it is possible to run a particular program automatically whenever the raspberry pi completes booting (i.e. raspberry pi is switched 'on') without requesting any login id or password...

Re: Running a program automatically as the Raspberry pi boot

Posted: Tue Dec 31, 2013 12:30 pm
by shuckle
/etc/rc.local is executed during the boot as root. Add your commands there and they will be run as root.

Re: Running a program automatically as the Raspberry pi boot

Posted: Thu Jan 02, 2014 10:50 am
by VISWAKUMARAN
Thank you for your reply... Can you please tell me how should I call the "Image.py" file (which contains the codes that must be executed as soon as the raspi boots up) from the "/etc/rc.locale"...???

Please send me a sample code...

Thank you shuckle...

Re: Running a program automatically as the Raspberry pi boot

Posted: Thu Jan 02, 2014 11:50 am
by shuckle
python image.py

Quite similarly than if you call it from any shell (terminal) program. Except that you need to use full paths.

Re: Running a program automatically as the Raspberry pi boot

Posted: Fri Jan 10, 2014 3:10 pm
by tfpereira
Unless you really have a reason to run your application with superuser previleges, I'd recomend using

Code: Select all

su pi -c python image.py 
instead, supossing "pi" is your default, non-super user previleged user.

Re: Running a program automatically as the Raspberry pi boot

Posted: Fri Jan 10, 2014 3:51 pm
by Richard-TX
Every startup script is run by root which makes sense if you think about it.

Most of the daemons that do not need root privs su to a different user upon execution.

If your program needs root privs then there is no special changes.

Having said that, if your program does not read any data from users, then it is acceptable to let it run as root.

Put another way. if user "ausr" hacks the account "lusr" then any process owned by "luser" is at risk.


For those critical apps that run in a hostile environment, I lock the pi account, create a new one, assign a good passwd to root and use su, not sudo as needed. Put another way, sudo is a security risk as it requires no password. Might as well just log in as root and be done with it.

Re: Running a program automatically as the Raspberry pi boot

Posted: Thu Jan 30, 2014 12:09 pm
by VISWAKUMARAN
Hello friends thanks for clarifying my doubts. It was really useful for me... I got my code running efficiently... :D
I have a doubt... :|
If I have to stop the program from executing after sometime and to bring the RPi to halt, is it possible to do it by using the os command sudo halt inside the python code... :?:
If there is any other better solution, please let me know... :)

Re: Running a program automatically as the Raspberry pi boot

Posted: Thu Jan 30, 2014 12:27 pm
by hk1963
I've also such an application and decided to run it from the user's crontab with an @reboot entry.
I think it is simpler this way.

Re: Running a program automatically as the Raspberry pi boot

Posted: Tue Feb 04, 2014 7:56 pm
by Craynerd
I would also recommend crontab and if needing to run as su, sudo crontab: crontab -e

@reboot python /path/to/file.py &

Re: Running a program automatically as the Raspberry pi boot

Posted: Tue Feb 04, 2014 11:28 pm
by sej7278
write an LSB init script using start-stop-daemon manpage