Executing a script on startup using BeagleBone Black
Asked Answered
H

3

8

I have an a.out which I want to run when my BeagleBone boots up. It is actually a socket server which I want to start as soon as the BeagleBone powers up. I tried to put this in /etc/init.d, but it didn't help. I wrote a shell script to run this executable but even that did not help.

What can I do to make a script run as soon as it boots up?

Hedwig answered 4/3, 2015 at 12:37 Comment(3)
Welcome to Stack Overflow! I've reworded your question such that it is easy to pick out and I've linked to the board just in case anyone isn't familiar with it.Bobbinet
What exactly Debian distribution (and version) you are using on your BBB? I'm asking, because what you need to figure out first, is which init system is in use for your distribution. As far as I remember, some versions of BBB comes with systemd. If it's your case -- you need to figure out how to create init script for systemd, or replace it with sysv-init. Also, see this question.Chloris
I found a solution , I wrote a service in /lib/systemd to run my a.out file on power-up , it works fine . Anyways , thanks for the help SamHedwig
N
12

It took me quite some time to figure this out, but with lots of research, I finally found what I was looking for.

  1. Compile the required code.

  2. Create a bash script that will launch the code at boot/ startup

    cd /usr/bin/
    

    Type nano scriptname.sh

    #!/bin/bash
    /home/root/name_of_compiled_code
    

    Save and grant execute permission

    chmod u+x /usr/bin/scriptname.sh
    
  3. Create the service

    nano /lib/systemd/scriptname.service
    
  4. Edit the above file as necessary to invoke the different functionalities like network. Enable these only if the code needs that particular service. Disable unwanted ones to decrease boot time.

    [Unit]
    Description=description of code
    After=syslog.target network.target
    [Service]
    Type=simple
    ExecStart=/usr/bin/scriptname.sh
    [Install]
    WantedBy=multi-user.target
    
  5. Create a symbolic link to let the device know the location of the service.

    cd /etc/systemd/system/
    ln /lib/systemd/scriptname.service scriptname.service
    
  6. Make systemd reload the configuration file, start the service immediately (helps to see if the service is functioning properly) and enable the unit files specified in the command line.

    systemctl daemon-reload
    systemctl start scriptname.service
    systemctl enable scriptname.service
    
  7. Restart BBB immediately to see if it runs as intended.

    reboot
    

(All credit goes to http://mybeagleboneblackfindings.blogspot.com/2013/10/running-script-on-beaglebone-black-boot.html)

Nagari answered 10/7, 2015 at 14:54 Comment(0)
P
3

I followed the 7 steps to create a service but it doesn't work for me, then I put my shellscript commands to run my program, in the /etc/rc.local and it worked.

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

cd /home/my_program_directory
/home/my_program_directory/my_executable

exit 0
Passer answered 4/8, 2017 at 18:19 Comment(0)
V
0

You simply need to add the /etc/rc.local file and get it to run your stuff like this:


Add the file /etc/rc.local and make it executable

touch /etc/rc.local
chmod a+x /etc/rc.local

Add code to run your code to /etc/rc.local

#!/bin/bash

cd /home/debian
bash -c './a.out &>> a.log' &

exit 0

Put your binary in /home/debian/ on the device. Then reboot and observe it running in the log /home/debian/a.log


Why does this work? Because even though the /etc/rc.local is not added to base the BBB the facility is still present. You just need to add the file and it'll work as with all Linux systems.

You can see this if, before adding it, you run systemctl status rc-local.service

root@beaglebone:~# systemctl status rc-local.service
Warning: The unit file, source configuration file or drop-ins of rc-local.service changed on disk. Run 'systemctl daemon-reload' to reload units.
* rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
        --debian.conf
Active: inactive (dead)
    Docs: man:systemd-rc-local-generator(8)

See Active: inactive (dead) because the file /etc/rc.local is missing

Vanesavanessa answered 31/12, 2022 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.