Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
  • News
  • Artificial Intelligence
  • Applications
  • Linux
No Result
View All Result
Cloudbooklet
No Result
View All Result
Home Linux

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

by Hollie Moore
3 months ago
in Linux
&Quot; Systemd Service On Linux
ShareTweetSendShare
Readers like you help support Cloudbooklet. When you make a purchase using links on our site, we may earn an affiliate commission.

Learn how to create a new systemd service on Linux with this step-by-step guide. Automate tasks and boost productivity on your Linux system.

ADVERTISEMENT

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.

Table of Contents

  1. Prerequisites
  2. Steps to Create a New systemd Service
    1. Make a Service File
    2. Setting Up the Service File
    3. Enable and Start the Service
  3. Unit Types of systemd Service
  4. Conclusion

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:

ADVERTISEMENT

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.

You might also like

Symbolic Links In Linux

Symbolic Links in Linux: What They Are and How to Use Them

2 months ago
Ubuntu Password

Reset your Forgotten Ubuntu Password in 2 Minutes

2 months ago

Launch a terminal or command prompt.

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

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

ADVERTISEMENT

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.

Service:

ADVERTISEMENT

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:

ADVERTISEMENT

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:~$

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

Unit TypeDescription
serviceA service on the system that includes instructions for starting, restarting, and halting the service.
socketA network socket that is linked to a service.
deviceA device that is explicitly handled by systemd.
mountA mountpoint that is maintained by systemd.
automountAt boot, a mountpoint is automatically mounted.
swapChange the system’s space.
targetA point of synchronisation for other units. Typically used to launch enabled services when the computer boots.
pathA path for activation depending on a path. You can, for example, launch services based on the state of a specific path, such as whether it exists or not.
timerA timer that allows you to arrange the activation of another unit.
snapshotA snapshot of the present condition of systemd. Typically used to undo temporary changes to systemd.
sliceResource restriction via Linux Control Group nodes (cgroups).
scopeData from systemd bus interfaces. Typically, it is used to handle external system processes.
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.

Tags: Linux
Share1Tweet1SendShare
Hollie Moore

Hollie Moore

Greetings, I am a technical writer who specializes in conveying complex topics in simple and engaging ways. I have a degree in computer science and journalism, and I have experience writing about software, data, and design. My content includes blog posts, tutorials, and documentation pages, which I always strive to make clear, concise, and useful for the reader. I am constantly learning new things and sharing my insights with others.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

List Groups In Linux

How to List Groups in Linux: A Guide for Beginners

3 months ago
Hostname In Linux

How to Modify the Hostname in Linux

3 months ago
Linux Systems

Linux systems Hacked with OpenSSH Malware

3 months ago
Install Iptables On Ubuntu

How to Install Iptables on Linux

3 months ago

Follow Us

Trending Articles

Cloud Vps Server

Top 10 Advantages of a Cloud VPS Server

September 19, 2023

10 Best AI Song Generator in 2023 (Free and Paid)

HeyGen AI: Free AI Video Generator to Create Amazing Videos

Create High Quality AI Cover Song with Covers AI

7 Best AI Girl Generators for Creating Realistic and Beautiful AI Girls

How to Delete Netflix Account Permanently

Popular Articles

Ai Image Inpainting

9 Best AI Image Inpainting: Create and Recreate Stunning Images

September 6, 2023

Top 10 AI Presentation Maker: Easy Way to Create Stunning Presentations

7 Best Deepswap AI Free Online Tools to Create FaceSwap Videos and Photos

10 Apps to See Who Viewed Your Instagram Profile

AI Emotion Detector: Detect and Interpret Human Emotions Accurately

Bland AI Phone Calls API: A Simple Way to Automate Phone Calls

Subscribe Now

loader

Subscribe to our mailing list to receives daily updates!

Email Address*

Name

Cloudbooklet Logo

Welcome to our technology blog, where we explore the latest advancements in the field of artificial intelligence (AI) and how they are revolutionizing cloud computing. In this blog, we dive into the powerful capabilities of cloud platforms like Google Cloud Platform (GCP), Amazon Web Services (AWS), and Microsoft Azure, and how they are accelerating the adoption and deployment of AI solutions across various industries. Join us on this exciting journey as we explore the endless possibilities of AI and cloud computing.

  • About
  • Contact
  • Disclaimer
  • Privacy Policy

Cloudbooklet © 2023 All rights reserved.

No Result
View All Result
  • News
  • Artificial Intelligence
  • Applications
  • Linux

Cloudbooklet © 2023 All rights reserved.