Implementing a Service With the Debian Package on Raspberry Pi


Raspberry Pi board

Learn more about implementing a service with the Debian package on a Raspberry Pi.

If you have a Raspberry Pi, you must have used it for many different projects. Most often, developers use a Raspberry Pi to run a web service with popular languages such as Python.

But it is not a server with redundancy and would most likely undergo reboots. In that case, you do not want the services to go down each time.

You may also like: How to Set Up Your Minecraft Server on Debian or Ubuntu

There are two ways to approach this problem:

1. Manually make sure that you hook it to systemd or init.d 

2. Automate it using a package that you deploy on any Pi/Debian machine

With the second approach, you can share your service with your friends and colleagues. So, here is a step-by-step guide on how to create a Debian package for Raspberry Pi. Let’s get started!

Getting Started

Before we get started, there are a few requirements:

  • Computer — the steps listed below are using a MacBook, but it does not matter as long as you can install the dkpg-deb on it.

  • Raspberry Pi or any other Debian machine

That’s it! 

Step 1

Install  dpkg-deb, which is a utility from the Debian package management suite. For more information, check out the Wiki page. On a Mac, it is quite straightforward if you have brew installed.

brew install dpkg

Once the command finishes executing, you will see a set of new commands available. For this guide, we are interested in dpkg-deb.

Step 2

Next, we need to get the workspace ready to create a Debian package. To create a simple Debian package, you need to organize your workspace so that the required files are placed at expected locations post installation.

For example, if you have a binary, which you want to be available as a command, you would need to put it into the $PATH. Let’s say we have to place it in /usr/local/bin.

In our case, we built a binary for an agent to send metrics from our Raspberry Pi to a webserver on the cloud. This is just an example.

Name of the binary: http-client  

So, in your workspace, create a root folder with the name of the package you want:

mkdir alphamon-agentpi_0.0-1
mkdir alphamon-agentpi_0.0-1/usr
mkdir alphamon-agentpi_0.0-1/usr/local
mkdir alphamon-agentpi_0.0-1/usr/local/bin

alphamon-agentpi_0.0_1  would be at the end name of your package.

NOTE: Standard version applies for 0.0_1, Major.Minor_Revision  

Now, copy the binary:

cp http-client alphamon-agentpi_0.0-1/usr/local/bin

With this, once you’ve created a package, make sure it will be placed in /usr/local/bin.

Step 3

The next step is to add details about the package so that anyone using it can know more about it.

Create a folder debian under the root folder:

cd alphamon-agentpi_0.0-1/
mkdir bebian

Now, create a file named control and add the lines below:

Package: alphamonagent
Version: 0.0-1
Section: base
Priority: optional
Architecture: armhf
Maintainer: Sameer <>
Description: Agent to support device discovery and reporting of AlphaMon

More information about each field is available here. All of the fields are self-explanatory.

Step 4

Now that we have a binary in the path, we need to make sure that binary (which is a small service in this case) is called every time on boot up.

For this, we rely on systemd, which is a fairly common service manager for embedded systems and is supported in most operating systems installed on Raspberry Pi.

We use the existing system-v to systemd conversion to make our lives easy.

First, we create a shell script, which would be put at /etc/init.d/. 

start() { # code to start app comes here echo "hello alphamonagent here" } stop() { # code to stop app comes here echo "hello alphamonagent here" } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) # code to check status of app comes here # example: status program_name ;; *) echo "Usage: $0 {start|stop|status|restart}"
esac exit 0 

A fully working example can be found here.  

Step 5

In case you want some actions to be performed before or after installation, you need to add  preinst/postinst  files.

A sample postinst file that enables the service we created in /etc/init.d in step four can be found below:

#!/bin/sh echo "\033[36m HI I'M A POSTINST SCRIPT `date +"%s"` \033[39m"
systemctl enable alphamonagent
systemctl start alphamonagent exit 0

Step 6

We are all set; now, we just need to execute a simple command to create the Debian package.

Navigate to your workspace root folder.


Then, execute the following:

dpkg-deb --build alphamon-agentpi_0.0-1/

Voila! You have the Debian package alphamon-agentpi_0.0-1.deb!

Last Step

Install the deb package on your Raspberry Pi:

dpkg -i <package_name>

As we added commands to enable and start the service in the postinst maintainer script, the service automatically starts, and after every reboot, it gets started on its own.

The code used above is available on GitHub.

Further Reading

How to Set Up Your Minecraft Server on Debian or Ubuntu

Minideb: A Minimalist, Debian-Based Docker Image

This UrIoTNews article is syndicated fromDzone