How to Daemonize an Application with Systemd
Last updated: July 19th 2023
Introduction
A daemon is a program that runs continuously as a background process and wakes up to handle periodic service requests. These requests often come from remote operations, and the daemon program is alerted to them by the operating system (OS). The daemon either responds to the request or forwards it to another program or process as appropriate.
Creating a systemd unit file is essential if you want Ubuntu's init system to start your application automatically. This guide will show you how to make the systemd service unit file so your application can start without any issues by illustrating how you can daemonize Gunicorn and Flask applications to begin whenever the server boots.
Prerequisites
- An application (I use a Flask Application with Gunicorn and Nginx, refer to this guide)
- A Linux server with a Sudo user (I use ubuntu in this tutorial)
Creating a systemd service file
To begin, create a unit file that ends in .service within the /etc/systemd/system directory where python-server can be any name:
$ sudo nano /etc/systemd/system/python-server.service
Inside the [Unit] section, you'll specify metadata and dependencies. Add a description of your service here, and tell the init system only to start this after the networking target has been reached:
[Unit] Description=I serve python-server After=network.target
Next, add a [Service] section to your file. This will specify the user and group under which you want the process to run. Give your regular user account ownership of the process since it owns all relevant files. Give group ownership to the www-data group so Nginx can communicate easily. Replace the username here with your username.
[Service] User=admin Group=www-data
Next, map out the working directory and update the PATH environmental variable (if your application executables are located in a non-standard path) so that the init system knows where to find the executables for the process. Also, specify the command to start the service. This will help ensure that your process starts up correctly and smoothly:
WorkingDirectory=/home/admin/ ExecStart=/usr/bin/gunicorn -w 5 python-server:app
And Finally, we'll add a [Install] section which tells the systemd what to do when we enable this service to start at boot. We want the service to start automatically when the system starts up, so we'll specify that here:
[Install] WantedBy=multi-user.target
In the end, your service file looks like this.
Your systemd service file is complete, and now is the time to save and close it.
Start your SystemD file
You can now start the service that you created. To do this, enter the following command:
$ sudo systemctl enable python-server $ sudo systemctl start python-server
Not sure if it is working? Check the status by running the following command:
$ sudo systemctl status python-server
(Optional) Restart your Server
This is optional, but to check if our Application has been daemonized, simply restart your server. If you don’t get an error on your IP address or domain upon restarting your server, you have successfully daemonized your application.
Conclusion
This article outlined steps on How to Daemonize an Application using Flask web application with Gunicorn and Nginx as an example.
Aayush Nair has been designing WordPress websites for eight years now. He is also a content writer, writing blogs on technology and related topics, and likes helping people in his free time.
Related articles
-
How to install OpenLiteSpeed on Webdock
In this article we show you how you can install the OpenLiteSpeed web server on Ubuntu Jammy.
-
How to Deploy your First Node.js Application on your Ubuntu Web Server
-
How to set up WireGuard on your Webdock Server
This article details how you can quickly and easily install WireGuard on your Webdock Server.
-
How to set up OpenVPN on your Webdock Server
This article details how you can quickly and easily set up a VPN on your Webdock server.
-
How to Install and configure aaPanel on Ubuntu
-
How to install azuracast on Webdock
This guide shows you how to work around certain issues when installing azuracast on Webdock.
-
How to set up Runcloud on Webdock
-
How to set up cPanel on Webdock
-
How to set up Gridpane on Webdock
-
How to set up Ploi on Webdock
This article details the steps you need to go through in order to install Ploi on a Webdock server.
-
How to set up Laravel Forge on Webdock
-
How to set up Plesk on Webdock
This article details the steps you need to go through in order to install Plesk on a Webdock server.
-
How to set up Cyberpanel on Webdock
-
How to set up SpinupWP on Webdock
-
How to set up DirectAdmin on Webdock
-
How to set up Hestia on Webdock
-
How to set up Virtualmin on Webdock
-
How to install and create pipelines in Jenkins
-
Basic WordPress site setup with aaPanel
In this guide, we will install and setup a basic WordPress site with aaPanel.
-
How to use Nginx as reverse proxy and secure connections with SSL certificates
Using Nginx to proxy pass your site with SSL security.
-
Setting up monitoring with Netdata on your Webdock server
-
How to Setup Python Web Application With Flask Gunicorn and Nginx
A simple Python Flask web app hosting with Gunicorn and Nginx
-
Set-up New Relic Monitoring on Your Webdock Server
This guide provides step-by-step instructions to install New Relic to monitor your VPS.
-
Getting Started with Ruby on Rails on Webdock
In this guide, we will show you how to get started with Ruby on Rails on your Webdock server
-
How to Install VaultWarden on Your Webdock Server
-
How to Install the Latest Version of HTOP on Ubuntu Server
Instructions to install latest htop package on your Ubuntu server
-
How to Install ImageMagick 7 on Ubuntu LEMP/LAMP stacks
Simple instructions to install ImageMagick 7 along with the PHP extension
-
A Quick Guide to Installing Rust on Ubuntu
-
How To Install Proxmox on Your Webdock Server
This article provides instructions on how to install Proxmox on your Webdock server
-
How To Run Nextcloud on Your Webdock Ubuntu Server
Instructions to Install Nextcloud on your server with Docker
-
The Ultimate Guide to Setting Up Mastodon server
A detailed guide with instructions to set up Mastodon on your Webdock server
-
A Guide To Setting Up Mindustry Game Server on Ubuntu
Step-by-step instructions to set up your own Mindustry server
-
A Guide to Installing ERPNext on Your Webdock Server
Step-by-step instructions to install ERPNext - a resource planning software - on your server
-
A Beginner's Guide to Installing Apache Kafka on Ubuntu
Detailed instructions to get Kakfa running on Ubuntu!
-
A Quick Guide To Installing Rocket Chat on Ubuntu
Instructions to install Rocket Chat on your Ubuntu server
-
How to Install Ghost CMS on a Webdock Server
Easily install Ghost CMS on your Webdock server with these instructions!
-
How to Install cPanel on a Webdock Ubuntu Server
Instructions for setting up cPanel on an Ubuntu server