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

A Week in the Life of an Invoice Wrangler: Navigating Ridesharing and Food Delivery Chaos

As an app founder in the ridesharing and food delivery industry, I found myself knee-deep in invoice reports from companies like Bolt, Uber, Glovo, and Bolt Food

Free HTML templates list for Startups

Free HTML templates list for startup. A complete list with free resources to build your next startup's website and gain the traction to the sky.

Deal with client requests in SaaS

How to deal with client requests in Saas which are seeing only their interests, not the product interest.

The first client of LoyalXpert is not answering anymore

Trying to implement LoyalXpert app, I lost my first customer, he's not answering anymore

Experiments with Tiktok Ads

Recently tried out TikTok ads for the first time and here are some of my learnings and challenges

People don’t care about you, until they know you care about them.

People don’t care about you, until they know you care about them. The same happens in business, you need to take care of your clients.

The One Word That Can Ruin Your SaaS Business Anyone

As a SaaS founder, you probably know how important it is to have a clear and specific target market for your product.

How I got my digital certificate connected it with ANAF

How I got my digital certificate from certSIGN and connected it with ANAF

The Ultimate List of Company Directories to Boost Your Networking

Discover a wide range of company directories to boost your business's visibility and connect with potential clients.

Discover the Best Free AI Art Tools for Your Next Masterpiece

Explore a curated collection of the finest free AI art tools, designed to help you bring your artistic vision to life.