5

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?

elixenide
  • 210
  • 1
  • 5
  • 9
nreich
  • 108
  • 1
  • 4

3 Answers3

6

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.

gurcanozturk
  • 3,748
  • 1
  • 19
  • 17
1

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.

Patrick Cook
  • 6,365
  • 7
  • 37
  • 63
jeffresc
  • 199
  • 4
  • If his program needs sudo privileges then the command needs to be sudo 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 as root, that is why there is a user (-u) parameter. sudo doesn't need to be in the crontab file. He just needs to edit root's crontab file with sudo crontab -e. If he entered crontab -e he would only edit his user's crontab 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
1

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".

Kolban
  • 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