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.
-
Beware that if the command is persistent (i.e., it is going to run for more than a moment) you should use
command &
-- the&
will fork it into the background properly. For troubleshooting: raspberrypi.stackexchange.com/questions/40493/…– goldilocks ♦Commented Jan 23, 2016 at 22:25 -
@goldilocks What command would I use and how can I edit this file?– HumanCommented Jan 23, 2016 at 22:31
-
1@Human The command would be the path to the program you want to run, e.g.,
/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 ♦Commented Jan 23, 2016 at 22:46 -
What would be a great editor for a noob like me? (Just want to do simple editing)– HumanCommented Jan 23, 2016 at 22:51
-
A lot of people use
nano
for stuff like this; it works without a GUI and is already installed.– goldilocks ♦Commented Jan 23, 2016 at 22:56
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.
-
If his program needs
sudo
privileges then the command needs to besudo crontab -e
. Commented Feb 5, 2016 at 3: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.– jeffrescCommented Feb 5, 2016 at 4: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. Commented Feb 5, 2016 at 4:02 -
@PatrickCook I was referring that crontab is executed as root when adding appending sudo to it.– jeffrescCommented Feb 5, 2016 at 4: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".
-
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. Commented Feb 5, 2016 at 4:27