So, I put my Pi on a custom quadcopter. I have a program that will track an object with a cam and send out pulse widths to control my motors. Anyway, right now I have to ssh into my Pi to run my executable. I am using a WiFi dongle to connect with ssh, but that thing is sucking a lot of current, so I need to get rid of it. I would like it to run my makefile executable on power up, instead of having to ssh in and run it each time I want to fly. I already have the Pi auto-logging in. The makefile is compiled and ready to go with sudo ./MyProg
. Any thoughts?
3 Answers
Put your command into the /etc/rc.local
file. You don't have to use sudo because this file is run by init at boot time.
If you encounter problems doing this, see:
If the program is persistent (i.e., keeps on ticking) be certain to background it. If it does not have such a feature built in, use &
at the end of the line. If you do not do this, likely your boot will snag for a bit then init will kill the process.

- 3,748
- 1
- 19
- 17
You can just run sudo crontab -e
and add the @reboot
prefix to the command. For example if you wanted to run bash /home/pi/script.sh
on boot, simply add @reboot bash /home/pi/script.sh
to the end of your crontab file. Remember that crontab is user specific and may require sudo and a full path of execution.

- 6,365
- 7
- 37
- 63

- 199
- 4
-
If his program needs
sudo
privileges then the command needs to besudo crontab -e
. – Patrick Cook Feb 05 '16 at 03:59 -
Yes, but once in the file it will not be necessary to add it again to your command as the crontab is already running it as root. – jeffresc Feb 05 '16 at 04:00
-
@jeffesc
crontab
does not run asroot
, that is why there is a user (-u
) parameter.sudo
doesn't need to be in thecrontab
file. He just needs to editroot
'scrontab
file withsudo crontab -e
. If he enteredcrontab -e
he would only edit his user'scrontab
file. – Patrick Cook Feb 05 '16 at 04:02 -
@PatrickCook I was referring that crontab is executed as root when adding appending sudo to it. – jeffresc Feb 05 '16 at 04:45
For starting programs at boot time, another solution is to use systemd
. This has the advantage of starting and stopping the program as well as just start on boot. A sample video tutorial illustrating how to use systemd is available here:
https://www.youtube.com/watch?v=eEuViHanjKI
There are also many other resources available by searching on "systemd".

- 1,784
- 2
- 16
- 28
-
BE WARNED this solution only works on Raspbian Jessie. As
systemd
was introduced in that flavor. This question was posted in 2014, and Jessie released in 2015, so it's fair to assume the OP was using Raspbian Wheezy. Though, if you are using Jessie, this is the best solution. – Patrick Cook Feb 05 '16 at 04:27
command &
-- the&
will fork it into the background properly. For troubleshooting: http://raspberrypi.stackexchange.com/questions/40493/log-output-of-system-script – goldilocks Jan 23 '16 at 22:25/home/pi/myprogram.py &
. The file requires root permission to edit, trysudo [editor] rc.local
where "[editor]" is whatever editor you want to use. – goldilocks Jan 23 '16 at 22:46nano
for stuff like this; it works without a GUI and is already installed. – goldilocks Jan 23 '16 at 22:56