I've got a beaglebone black that has Debian 9.1. we wrote a c++ program for its GPIOs and We want this program to always run when the system turns on. how can we do that?
An extremely simple solution would be to add a
@reboot cron job that just runs the binary.
crontab -e for the user that needs to run the code (e.g.
sudo crontab -e for roots crontab), and add the line
This will schedule the job to run each time the system has booted up.
crontab(5) manual for more info (
man 5 crontab).
Depending on what the program does, this may be enough, or it may be too simplistic.
You can compile the program to say
run_gpio and save it in a known path, e.g.
/path/to/run_gpio. It has to be marked executable for root:
$ sudo chmod 755 /path/to/run_gpio
Then configure Debian's
init system to start your program.
You can find an explanation e.g. here: http://xmodulo.com/how-to-automatically-start-program-on-boot-in-debian.html
In Debian, a directory called /etc/init.d contains a list of scripts that are executed by init process during startup and shutdown. Thus, in order to automatically run a particular program or script at startup, you can create a corresponding init.d script.
$ sudo vi /etc/init.d/run_gpio
Instead of vi, you can also use nano.
Put these lines in the new file within the init.d directory:
case "$1" in start)
Finally, make the init.d script executable, and add the init.d script to a default runlevel, so that the script can be called at boot time (and also during shutdown).
$ sudo chmod 755 /etc/init.d/run_gpio
$ sudo update-rc.d run_gpio defaults