Matt Richardson

Creative Technologist

BeagleBones

Creating Ångström System Services on BeagleBone

This is an adapted excerpt from Getting Started with BeagleBone. Throughout the book, you'll launch projects by executing your code from the command line. However, there are many circumstances in which you'll want the BeagleBone to start your code immediately after it boots up. This is called a system service. For instance, in a temperature logging project in chatper 6, you learn how to create a utility to upload the current temperature to the Xively Internet of Things platform every 20 seconds. After you launch it from the command line, it did just that until you exit the program, log out, or power down the BeagleBone.

Ångström uses systemd to manage all the system services. To change systemd's settings, you'll use the systemctl command to enable, disable, start, stop, restart, and check the status of system services. Each service is defined by a .service file.

Creating a Service File

If you change into the /lib/systemd/system directory and list its contents you'll see all the different .service files. These services aren't necessarily enabled, but many are. First you'll create a new .service file for your code in /lib/systemd/system. I'll set a Python script called xively-temp.py in root's home directory as a system service in the example below.

root@beaglebone:~# cd /lib/systemd/system
root@beaglebone:/lib/systemd/system# nano xively-logger.service

In the new file you've created, put in the following text:

xively-logger.service

[Unit]
Description=Xively client

[Service]
WorkingDirectory=/home/root/
ExecStart=/usr/bin/python xively-temp.py
SyslogIdentifier=xively
Restart=on-failure
RestartSec=5


[Install]
WantedBy=multi-user.target

Let's take a look at what each part of this file means:

[Unit]
Description=Xively client

A human-readable description of the service.

[Service]
WorkingDirectory=/home/root/

The location of the file you're launching.

ExecStart=/usr/bin/python xively-temp.py

Command to execute within that location.

SyslogIdentifier=xively

Service name for system logs.

Restart=on-failure

If the application exits with an error code, try relaunching it.

RestartSec=5

Keep trying to restart the application every 5 seconds if it exits with an error code.

[Install]
WantedBy=multi-user.target

When enabled, launch the service towards the end of the boot process (specifically, when the system is ready for multiple users to log into it).

Enabling and Starting the Service

After saving the file, enable the service with the command systemctl enable:

root@beaglebone:/lib/systemd/system# systemctl enable xively-logger

Now your service will start when the BeagleBone has booted up, but it's not running right now. To start it immediately, use the command systemctl start:

root@beaglebone:/lib/systemd/system# systemctl start xively-logger

If you want to restart the service, use systemctl restart:

root@beaglebone:/lib/systemd/system# systemctl restart xively-logger

Disabling and Stopping the Service

If you want to disable the service from starting up at boot time, you can run systemctl disable:

root@beaglebone:/lib/systemd/system# systemctl disable xively-logger

If the service was already running, disabling it won't stop it. Stopping the service is as easy as using systemctl stop:

root@beaglebone:/lib/systemd/system# systemctl stop xively-logger

Checking the Status of a Service

You can always check the status of a system service by using systemctl status:

root@beaglebone:/lib/systemd/system# systemctl status xively-logger
   Loaded: loaded (/lib/systemd/system/xively-logger.service; enabled)
   Active: active (running) since Wed 2013-07-24 14:57:03 EDT; 4s ago
   Main PID: 652 (python)
   CGroup: name=systemd:/system/xively-logger.service
   `-652 /usr/bin/python xively-temp.py

Jul 24 14:57:03 beaglebone systemd[1]: Starting Xively client...
Jul 24 14:57:03 beaglebone systemd[1]: Started Xively client.

You can also list enabled services by typing systemctl by itself:

root@beaglebone:/lib/systemd/system# systemctl

Type space to page through the list and q to go back to the command line.