Sources
Shamelessly stolen from:
http://mybeagleboneblackfindings.blogspot.com/2013/10/running-script-on-beaglebone-black-boot.html
Introduction
At some point you posed the question to yourself,
"How do I make the BeagleBone Black (BBB) micro computer act like a micro controller?"
Often times you will want your project to start running once the BBB boots up, like the Arduino. Don't fret! It is possible, though you will need to jump through some hoops and be cognizant of a few things.
An overview of the steps are:
-Compile your executable
-Create a shell script that calls your executable
-Create a service routine to be run at boot
Compiling and Directory
I shouldn't have to tell you how to compile your code. But you should know the pathname to your executable. Generally, the BBB starts in
/home/root/
Storing your executable in this directory or any subdirectory of this path will serve your purposes most of the time. Sometimes your code will try to do something, generally related to changing something outside your code, and it wont work. For example, I had executable.o that created and wrote to file.txt in the same directory:
/home/root/executable.o
/home/root/file.txt
When I ran the program from the command line:
./executable.o
It would run fine, create file.txt and write to it. When I ran the program at boot the file wouldn't be created let alone be written to. To fix the problem I moved the files out of the root directory:
/home/xuser/executable.o
/home/xuser/file.txt
My best guess is that the computer can't or isn't allowed make operations to itself, at least in the root directory.
Creating a Shell Script
Create a bash script to run the code at bootup. We will assume the working directory for this example is /home/root/.
nano /usr/bin/<script>.sh
Type the following into the file and save with ctl-o then exit with ctl-x. You may have this script run more than one executable by adding additional pathnames after the first line. They will be executed in order, but remember this is a micro computer not a micro controller so you can have multiple programs running at the same time.
#!/bin/bash
/home/root/<executable>
Grant execute permissions to the script:
chmod u+x /usr/bin/<script>.sh
Create a Service Routine
nano /lib/systemd/<script>.service
Type the following into
<script>.service then save and exit the file.
[Unit]
Description=<description of code>
After=syslog.target network.target
[Service]
Type=simple
ExecStart=/usr/bin/<script>.sh
[Install]
WantedBy=multi-user.target
Now create a symbolic link so the system can find the service:
cd /etc/systemd/system/
ln /lib/systemd/<script>.service <script>.service
The following commands will reload the configuration file, begin the service, then enable unit files specified in the command line.
systemctl daemon-reload
systemctl start <script>.service
systemctl enable <script>.service
Restart the BBB and watch your program run on its own! Well, you can't really see any output on a terminal window, but if you interact with the physical world you can see it in action.
shutdown -r now
Suppose your program runs infinitely, how do you stop it? You can view the processes running with:
top
Find the PID or process number of your executable type
k PID.