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?

  • You have to you use systemd unit for that. – direprobs Sep 17 '17 at 19:18

An extremely simple solution would be to add a @reboot cron job that just runs the binary.

Do crontab -e for the user that needs to run the code (e.g. sudo crontab -e for roots crontab), and add the line

@reboot /path/to/some/executable

This will schedule the job to run each time the system has booted up.

See the 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.

|improve this answer|||||

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:

#! /bin/sh
# /etc/init.d/run_gpio
case "$1" in start)
;; esac

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

|improve this answer|||||

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.