Starting the program on power up

General programming chat and advice for beginners

7 posts
by BobConder » Sat Mar 18, 2017 1:29 am
I'm sorry, this question must have been asked and answer a thousand times but I've looked all over the forums and can't find it. Ok, so now I have my code running in a Python 3 IDLE shell. But I need to unplug it from my display and keyboard and take it out into the field. I'l have power but that's all. I wan't to set it up to run until the power goes out then restart when it comes back on. I've looked at the config.txt script and read the README file in the boot location but nothing jumps out to me as being helpful. Maybe someone here can point me to one of those thousands of answers already posted.
User avatar
Posts: 3
Joined: Mon Mar 13, 2017 12:17 am
by thagrol » Sat Mar 18, 2017 4:37 pm
Have a look at /etc/rc.local

To run "program" at boot, insert
Code: Select all
/path/to/program &
above the line that reads
Code: Select all
exit 0

To run "program" as a non-root user try
Code: Select all
su - user /path/to/program &

The & is important. without it the boot process won't complete correctly (or at all).
User avatar
Posts: 183
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
by DougieLawson » Sat Mar 18, 2017 4:40 pm
Much better is to run it from a systemd service file. You can even get things like a watchdog process to restart an always running process on failure.

https://www.raspberrypi.org/forums/view ... 54#p921354
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 26887
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by BobConder » Mon Mar 20, 2017 3:03 pm
Thank you for your suggestions but I don't seem to be able to put anything in etc directory. It doesn't recognize me as root. And I've tried changing the password in the config screen but it won't accept "raspberry" as my password. So I tried moving /etc to my /pi directory in order to work on it.... That clobbered the OS completely. Starting in "Safe" mode I had to reload the OS and start all over again. Still it wouldn't let me change the password nor allow me to put a file in etc. Its as if someone already changed to password on me. But this is a fresh download. When I go to the configuration screen it says I'm logged in as "raspberrypi". Shouldn't I be logged in as "Root"?
User avatar
Posts: 3
Joined: Mon Mar 13, 2017 12:17 am
by rpdom » Mon Mar 20, 2017 3:52 pm
BobConder wrote: When I go to the configuration screen it says I'm logged in as "raspberrypi". Shouldn't I be logged in as "Root"?

No, you should be logged in as "pi". To use root privileges on commands you can put "sudo" in front of them.

For example: "sudo nano /etc/rc.local" to edit the /etc/rc.local file as root.
User avatar
Posts: 10450
Joined: Sun May 06, 2012 5:17 am
Location: Essex, UK
by S0litaire » Mon Mar 20, 2017 4:54 pm
Just an idea...
Would it be easier ( better / worse??) to use a cron job instead?
https://www.raspberrypi.org/documentati ... ge/cron.md

You can have a cronjob with the flag "@reboot" which runs that cronjob at reboot.
instead of the usual cron style of:
Code: Select all
* * * * *  /command/to/execute
you use
Code: Select all
@reboot  /command/to/execute
which will run every time the pi reboots/starts up.
--
Laters

Bill "Solitaire" C

Anáil nathrach, ortha bhas betha, do cheol déanta
Posts: 129
Joined: Thu Dec 29, 2011 4:24 pm
Location: Ayrshire, Scotland
by Heater » Mon Mar 20, 2017 6:08 pm
It would be easier to use a systemd service file. Makes things easy to enable and disable or start and stop at will. Also allows for restarting a service if it crashes out.

Now that you have trashed your system by moving /etc to some place else it's probably time to install a new raspbian image and start again.

I don't understand how you did not have permissions to edit files in /etc but did have enough permissions to move it all together!
Posts: 6908
Joined: Tue Jul 17, 2012 3:02 pm