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?

5

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 Jan 23 '16 at 22:25
  • @goldilocks What command would I use and how can I edit this file? – Human Jan 23 '16 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, try sudo [editor] rc.local where "[editor]" is whatever editor you want to use. – goldilocks Jan 23 '16 at 22:46
  • What would be a great editor for a noob like me? (Just want to do simple editing) – Human Jan 23 '16 at 22:51
  • A lot of people use nano for stuff like this; it works without a GUI and is already installed. – goldilocks Jan 23 '16 at 22:56
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.

  • If his program needs sudo privileges then the command needs to be sudo crontab -e. – Patrick Cook Feb 5 '16 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. – jeffresc Feb 5 '16 at 4: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 5 '16 at 4:02
  • @PatrickCook I was referring that crontab is executed as root when adding appending sudo to it. – jeffresc Feb 5 '16 at 4: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".

  • 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 5 '16 at 4:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.