Appendix B. Setting up System Services

Throughout this book, you’ve launched 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 Data Logging with Xively, you created a utility to upload the current temperature to the Xively server every 20 seconds. After you launched it from the command line, it did just that until you exited the program, logged out, or powered down the BeagleBone.

Debian 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.

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 the example from Example 6-11 as a system service in Example B-1.

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

In the new file you’ve created put in the following text from Example B-1:

Example B-1. Source code for /lib/systemd/system/xively-logger.service
[Unit]
Description=Xively client 1

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


[Install]
WantedBy=multi-user.target 7
1

A human-readable description of the service.

2

The location of the file you’re launching.

3

Command to execute within that location.

4

Service name for system logs.

5

If there’s an error when trying to start the application, keep trying.

6

Keep trying to restart the application every 5 seconds.

7

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.service

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.service

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

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

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.service

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.service

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.service
   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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.13.192