DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Heterogeneity of Computing Environments Using Cross-Compilation
  • How to Secure Your Raspberry Pi and Enable Safe, Resilient Updates
  • IoT Needs To Get Serious About Security
  • Recognizing Music Genres With the Raspberry Pi Pico

Trending

  • Chaos Engineering and Kafka In Action: Topic Level Idempotence and Retry Check
  • Building a Feature Store With Neo4j and Feast
  • Designing a Polling Pipeline for Rate-Limited APIs Without Losing Data
  • AI-Assisted Refinement Preparation

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. IoT
  4. Implementing a Service With the Debian Package on Raspberry Pi

Implementing a Service With the Debian Package on Raspberry Pi

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

By 
Sameer Chandra
·
Dec. 04, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

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 <[email protected]>
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.

ls
alphamon-agentpi_0.0-1


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

Minideb: A Minimalist, Debian-Based Docker Image

raspberry pi Debian Web Service

Opinions expressed by DZone contributors are their own.

Related

  • Heterogeneity of Computing Environments Using Cross-Compilation
  • How to Secure Your Raspberry Pi and Enable Safe, Resilient Updates
  • IoT Needs To Get Serious About Security
  • Recognizing Music Genres With the Raspberry Pi Pico

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook