How to Create a New systemd Service on Linux: A Step-by-Step Guide

Systemd is a powerful initialization system and service manager that is utilized by the vast majority of current Linux systems. It makes it easy to manage services, automate activities, and boost system efficiency.

In this article, we will lead you through the process of creating a new systemd service on Linux.

Prerequisites

  • A Linux distribution that has systemd installed (for example, Ubuntu, Fedora, or CentOS).
  • Access to the system as root or sudo.

Steps to Create a New systemd Service

There are various steps involved in creating a new systemd service on Linux. To set up your service, follow the steps below:

Make a Service File

The first step is to build the systemd service file, which defines the configuration and behaviour of your service. The service file must be located in the /etc/systemd/system/ directoryand must have a.service extension.

Launch a terminal or command prompt.

Create a new service file with a text editor. You can, for example, use the nano editor:

sudo nano /etc/systemd/system/<filename>.service

Define the sections and parameters in the service file based on your requirements. To get you started, here’s a basic template:

Description=Service for virtual machines hosted on VMware Documentation=http://open-vm-tools.sourceforge.net/about.php ConditionVirtualization=vmware DefaultDependencies=no Before=cloud-init-local.service After=vgauth.service After=apparmor.service RequiresMountsFor=/tmp After=systemd-remount-fs.service systemd-tmpfiles-setup.service systemd-modules-load.service [Service] ExecStart=/usr/bin/vmtoolsd TimeoutStopSec=5 [Install] WantedBy=multi-user. target Alias=vmtoolsd.service

Unit:

Provide a description of your service and any dependencies it requires in the [Unit] section. once=network.target, for example, ensures that the service begins once the network is operational.

See also  How to Enable Bing Chat Enterprise for Your Microsoft Search

Service:

Enter the command or program to be performed by ExecStart in the [Service] section. Set Restart to specify how systemd handles service restarts (for example, always, on-failure).

Install:

Define the target that should activate your service in the [Install] section. default.target is a popular option.

Setting Up the Service File

To demonstrate, let’s develop a sample service that uses Nmap to scan your machine’s ports and saves the results to a file every thirty seconds. Here’s an example of a service unit file structure:

[Unit] Description=Demonstration of custom nmap service. After=network.target [Service] Type=simple User=root ExecStart=/usr/bin/nmap -sS -O -oN /home/<user>/results.txt localhost Restart=always RestartSec=30 [Install] WantedBy=multi-user.target

In this example:

  • The Description gives a quick overview of the service.
  • After=network.target ensures that the service begins only after the network is operational.
  • A normal process service is indicated by type=simple.
  • Restart=always guarantees that the service is restarted when it terminates.
  • RestartSec=30 specifies a 30-second time between restarts.
  • User=root instructs the service to operate as the root user.
  • ExecStart contains the program’s command and any relevant arguments.

cloudbooklet@ubuntu:~$ sudo systemctl enable nmapper.service Created symlink /etc/systemd/system/multi-user.target.wants/nmapper.service _ /etc/systemd/system/nmapper.service cloudbooklet@ubuntu:~$ sudo systemctl start nmapper.service cloudbooklet@ubuntu:~$ sudo systemctl status nmapper.service _ nmapper.service – Demonstration of custom nmap service Loaded: loaded (/etc/systemd/system/nmapper.service; enabled; preset: enabled) Active: active (running) since Tue 2023-06-27 02:40:10 IST; 3s ago Main PID: 3160 (nmap) Tasks: 1 (limit: 4590) Memory: 53.4M CPU: 476ms CGroup: /system.slice/nmapper.service L160 /usx/bin/nmap -sS -A -0 localhost -oN /home/debxrshi/results.txt Jun 27 02:40:10 debian systemd[1]: Started nmapper.service – Demonstration of custom nmap service.. Jun 27 02:40:11 debian nmap[3160]: Starting Nmap 7.93 ( https://nmap.org ) at 2023-06-27 02:40 IST cloudbooklet@ubuntu:~$

See also  Midjourney tips and tricks using just one prompt

Enable and Start the Service

To enable and start your service, use the systemctl command. Take the following steps:

Allow the service to start automatically when the computer boots:

sudo systemctl enable <filename>.service

Start the service:

sudo systemctl start <filename>.service

Check the service’s status to confirm it is operational and error-free:

sudo systemctl status <filename>.service

Unit Types of systemd Service

Systemd Service

Also read: You might also find useful our guide on How to Open Port in Linux: Simple Step-by-Step Guide

Conclusion

Adding a new systemd service on Linux is a simple step that can dramatically improve automation and efficiency. You can easily develop custom services that run in the background and start automatically when the system boots by following the step-by-step guide. Give your Linux system self-executing tasks to reap the benefits of streamlined operations.

Please feel free to share your thoughts and feedback in the comment section below.