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.
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
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:
[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.
The location of the file you're launching.
Command to execute within that location.
Service name for system logs.
If the application exits with an error code, try relaunching it.
Keep trying to restart the application every 5 seconds if it exits with an error code.
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).
After saving the file, enable the service with the command
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
root@beaglebone:/lib/systemd/system# systemctl start xively-logger
If you want to restart the service, use
root@beaglebone:/lib/systemd/system# systemctl restart xively-logger
If you want to disable the service from starting up at boot time, you can run
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
root@beaglebone:/lib/systemd/system# systemctl stop xively-logger
You can always check the status of a system service by using
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: Starting Xively client... Jul 24 14:57:03 beaglebone systemd: Started Xively client.
You can also list enabled services by typing
systemctl by itself:
Type space to page through the list and q to go back to the command line.