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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Part 3 of My OCP Journey: Practical Tips and Examples
  • What Is Envoy Proxy?
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices

Trending

  • Part 3 of My OCP Journey: Practical Tips and Examples
  • What Is Envoy Proxy?
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
  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.

Sameer Chandra user avatar by
Sameer Chandra
·
Dec. 04, 19 · Tutorial
Like (6)
Save
Tweet
Share
17.40K 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.

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

How to Set Up Your Minecraft Server on Debian or Ubuntu

Minideb: A Minimalist, Debian-Based Docker Image

raspberry pi Debian Web Service

Opinions expressed by DZone contributors are their own.

Trending

  • Part 3 of My OCP Journey: Practical Tips and Examples
  • What Is Envoy Proxy?
  • From On-Prem to SaaS
  • Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: