Create systemd service - daemon (Ubuntu 15+)


Setup

Start by creating a file: /etc/systemd/system/CUSTOM_SERVICE_NAME.service with the following content:

[Unit]
Description=CUSTOM_SERVICE_DESCRIPTION

[Service]
ExecStart=/opt/www/EXEC_FILE
WorkingDirectory=/opt/www
Restart=always

[Install]
WantedBy=multi-user.target

Enable the service. It will start automatically on boot, after that.

$ sudo systemctl enable CUSTOM_SERVICE_NAME.service

Check status/start/stop/restart

$ sudo systemctl {status|start|stop|restart} CUSTOM_SERVICE_NAME

A complete list with all available directives that can be used inside the service file can be found here

Legend

  • CUSTOM_SERVICE_DESCRIPTION - a short description for the current service
  • CUSTOM_SERVICE_NAME - the name of service
  • EXEC_FILE - exec file name with the absolute path

Example 1 for a webservice written in Golang

You must have a new folder api in the root /home/ubuntu/. The main file it’s the binary file resulted after go compilation. In my example I use flags to set environment --env=dev, this is not mandatory.

[Unit]
Description= Go API Webservice Example

[Service]
ExecStart=/home/ubuntu/api/main --env=dev
WorkingDirectory=/home/ubuntu/api
Restart=always

[Install]
WantedBy=multi-user.target

Example 2 for a javascript

You must have create a web folder into /home/ubuntu/ root folder. The npm must be installed before and use the absolute path in ExecStart command, as you can see in the below example /usr/bin/npm.

[Unit]
Description=Javascript Web Application Example

[Service]
ExecStart=/usr/bin/npm run start
WorkingDirectory=/home/ubuntu/web/
Restart=always

[Install]
WantedBy=multi-user.target

Usefull command

$ service --status-all - display all services

  • [ + ] for running services
  • [ - ] for stopped services
  • [ ? ] for services without a ‘status’ command

Newsletter


Related Posts

Tools for Analyzing Your Competition

Before knowing where your business stands, you should look at the market and your competition. You need to know what marketing tactics and strategies are working for your audience, what traffic the competition has, and how they position.

Free Online Photo Editor with Cool Effects | Edit Images Easily

Transform your images with our free online photo editor with cool effects. Add filters, adjust colors, and apply creative enhancements. Edit your photos easily and make them stand out.

How to find the size of a table in SQL?

Learn how to determine the size of a SQL table with this informative guide. Discover efficient methods for measuring table size.

MongoDB Query date by _id field

Query date by _id in mongodb. Straighforward guide to solve your issue right away.

Unleashing the Power of Free API for Scrape Reverse Geocoding

Learn how to harness the potential of reverse geocoding with our beginner's guide to scraping APIs for free. Unlock the power today.

Discover the Power of Whatsapp-web.js for Safe and Easy Automation

Automate WhatsApp with ease using Whatsapp-web.js, a NodeJS client library that connects through the official WhatsApp Web app, reducing ban risks. Perfect for user or business accounts.

Website tours js tools

Tools to create live tours for your web project using js libraries.

Unlocking SaaS Marketing Success: Essential Resources to Learn and Grow

Discover a comprehensive collection of valuable resources to master the art of SaaS marketing.

Where to launch your startup

Do you wanna launch grandiosely your startup? Get a list of platforms where people can discover your project and can push your project directly to the sky.

Email campaign improving my SaaS convertion - Manager Flota

Users subscribe to your SaaS, but don't let the money on your table. Here's how I do it!