Start a C program when BeagleBone bootsPosted: April 11, 2012 | |
I’ve been spending some time lately trying to turn the BeagleBone into a “single-serving” system for my prototypes. Most of these prototypes require running some program whenever the system gets power. Before you continue, I strongly recommend you read my previous BeagleBone posts, if you start feeling a bit lost.
So here I have a very simple circuit… 5 LEDs connected to 5 different pins (P8_3,P8_4,P8_5,P8_11,P8_12). I would like these LEDs to display a perform a simple pattern whenever the BeagleBone is turned ON. Something like the following video.
The code for this circuit is pretty straight forward and looks like this:
#define TRUE 1
#define FALSE 0
int forever = TRUE;
void sighandler(int sig)
printf("CONTROL+C was caught... terminating in a friendly manner\n");
forever = FALSE;
struct gpioID selectedPins[nbr_selectedPins];
unsigned int data_to_write;
To compile and run this code, download my BeagleBone_IO library, save the above code as main.c and type the following commands:
gcc BeagleBone_gpio.o main.c -o main
What is neat about my code, is that it is able to trap a signal whenever the user types CONTROL+C and ends the program normally… after performing some generic BeagleBone GPIO cleanup.
Anyway, I would like this program to run whenever my BeagleBone starts up. BeagleBone’s default OS is Angstrom Linux which uses systemd as the initialization scheduler. I am still learning about this, but according to the wikipedia entry and also to Kezhong’s Weblog, this daemon is meant to efficiently express services dependencies, by allowing more services to be launched in parallel at system startup while reducing the need to mess around with many scripts. I used to do this on ubuntu… and it was a mess.
Anyway, I would like to declare my LED blinking program as a service. So I need to execute the following steps:
Step #1: Create a script that will call the program that you wish you launch at boot time
This means we need to create a file in the /usr/bin/ directory with any name. For example, assuming that our compiled LED program is in /home/root/main we can create an executable simple script called myScript.sh with the following lines of code.
echo “/home/root/main” >> /usr/bin/myScript.sh
chmod u+x /usr/bin/myScript.sh
Step #2: Create the service file
Create the file /lib/systemd/myScript.service . Actually your service name can be anything you want… not restrained to myScript.service.
Step #3: Write the appropriate service settings
Your /lib/systemd/myScript.service can be for example:
Description=Run the LED pattern script
Step #4: Make a symbolic link
ln /lib/systemd/myScript.service myScript.service
Step #5: Make systemd take notice of it, activate the service immediately and enable the service to be started on boot-up
systemctl start myScript.service
systemctl enable myScript.service
After executing all commands on step 5, the LED program should immediately start. Try switching the BeagleBone power OFF & ON… and you will see the LED program.
How to terminate the script that is constantly running in the background?
If everything went okay, your script in constantly running in the background. To terminate it, you need to kill it. Use the top command and find out the process id (pid) of your looping program. As shown on the screenshot, my main pid is 13.
The following command will take care of terminating the job.
How disable the script from starting up at the boot time?
Thanks to Piranha at the official BeagleBoard forum for guidance.