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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Merge HTML Documents in Java
  • Monoliths, REST, and Spring Boot Sidecars: A Real Modernization Playbook
  • Simpler Data Transfer Objects With Java Records
  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 user avatar
Sameer Chandra
·
Dec. 04, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
18.2K Views

Join the DZone community and get the full member experience.

Join For Free

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 <sss@gmail.com>
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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!