Saturday, October 26, 2013

Running a script on Beaglebone Black boot/ startup

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.
shutdown -r now

11 comments:

  1. Hj. When i realized step 6. My beagle appeared :"-bash : systemctl : command not found". I don't understand. Can you help me?

    ReplyDelete
  2. Hj. When i realized step 6. My beagle appeared :"-bash : systemctl : command not found". I don't understand. Can you help me?

    ReplyDelete
  3. Hello, I get error bad message when try to enable? you know to fix it?
    Thank you

    ReplyDelete
  4. @Tugas: Can you post the error you get? Which Operating System do you use? I suggest using Debian as lots of software packages and support is available for Debian than Angstrom.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. On my BeagleBoneBlack (Debian 7.9 wheezy), this worked with following changes:

    - in point 3. there was missing "system" in path. Correct command:
    nano /lib/systemd/system/<scriptname>.service

    - same in point 5. Correctly shall be:
    ln /lib/systemd/system/<scriptname>.service <scriptname>.service

    By the way - above command creates hard link, not symbolic. To create symbolic link, -s option must be used. Both symbolic and hard link work here anyway.

    Only mentioned changes in paths allow to correctly run command from point 7:

    systemctl enable <scriptname>.service

    Otherwise an error occurs "Failed to issue method call: no such file or directory"

    Thanks for tutorial!

    ReplyDelete
  7. what condition i need to add if i want to start after the x window

    ReplyDelete
  8. This works fine in root user .. What about non-root user??

    ReplyDelete
  9. Hello, how to revert that script runs on startup and start linux environment?

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete