I have written a program for wireless communication between raspberry pi and Arduin0 with Hc05. But how to run this program automatically after booting pi? every time I have to login pi and run the program as well as turn on the Bluetooth. How can I do all the staff automatically after booting pi
2 Answers
There are several ways to do this. IMHO, the two "best" ways are 1) systemd
, and 2) cron
. The tradeoffs (again, IMHO) are that systemd
is more comprehensive and technically superior, but more difficult (which only means that you'll need to invest more time learning before you can do useful things with it). I feel that cron
is easier, so that's the answer I'll propose below:
- From the command line, type
man cron
Read the manual (man page) for cron
. It's short, and you should have the background.
"Jobs" in
cron
are scheduled from a file that's called thecrontab
. You'll edit this file to add your "job". There's also a manual forcrontab
if you care to read it (you should):man crontab
Start the editor for
crontab
, and enter the instructions for starting your "job" on the command line of a terminal as follows:
(NOTE: I've added the$
symbol at the beginning of an input as shorthand for thebash
shell prompt)
$ crontab -e
Your default editor (likely nano
) will start, displaying the default contents of your crontab
. Move the cursor to the beginning of the first empty line at the bottom of the editor's display. Enter the following line:
@reboot /path/to/your/program > /home/pi/cronjoblog 2>&1
Save/Write the crontab
(^O
), then exit the editor (^X
) to return to the shell/command line.
You must use a complete file specifications in the crontab
entry. This is due to the fact that your crontab
is not executed with the same $PATH that you will have as user pi
. It's therefore a good habit to get into, even if it's not always strictly necessary. Note also the redirection of any stderr
(error messages) to the file /home/pi/cronjoblog
preserves this useful information for any post-op debugging that may be needed. Again, good habit.
- Reboot your RPi, and check if your program executed. If it did not, inspect the file
/home/pi/cronjoblog
for error messages that will help isolate the problem.
Occasionally, you will find that a job you've scheduled in your crontab
will persistently fail to execute, and the cause will be unclear. The issue may be due to the fact that cron
does not maintain awareness of whether programs and services have actually booted before it calls them. This is typically remedied by adding a brief sleep
delay to give these services time to boot before they are called. The following adds a 10 second delay to the previous line:
@reboot /bin/sleep 10; /path/to/your/program > /home/pi/cronjoblog 2>&1
cron
's lack of awareness is a shortcoming, and the primary reason that systemd
is a technically superior approach. Nevertheless, cron
remains useful and simple. Try this, and let us know how you get on via comments, or by editing your original question.
Just as a reminder, I'd like to add the "classic" solution as well. Just add your commands to the /etc/rc.local script! On newer systems, you might need to create it, but it works nevertheless. By default, it runs as root, so you might want to use it like this:
#!/bin/bash
sudo -u pi ~pi/bin/startup.sh >/dev/null 2>/dev/null &
Or something similar to use your default user. Watch out, because your usual environment is not set (e.g. your PATH), so either set them, or use absolute paths!
To know what is and isn't set, just add set >/home/pi/env.txt
to your script, and examine the file.
-
2Please take note that using
/etc/rc.local
has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it.– IngoMar 12, 2019 at 23:10