How to Create a New systemd Service on Linux
systemd is the service manager for a lot of well-known Linux distributions. A service is a unit of a program that typically runs in the background. Services, by nature, automatically start up as soon as the system boots up and can run unattended.
Sign up forfree
Forgot your password?
Create an account
*Required: 8 chars, 1 capital letter, 1 number
By continuing, you agree to thePrivacy PolicyandTerms of Use.You also agree to receive our newsletters, you can opt-out any time.

Step 1: Creating the Service File
There are a few steps required to create a new systemd service file. The first of them is creating the unit file for the service. Before you create a service file, you must understand its structure.
Let’s study the service file by pulling a real, working service file from your Linux system. Below, you can see the service file for the vmtools daemon service.

This particular service might not be present on your system unless you’re also running Linux on VMware with VMware tools installed. The service in question is not important because all services, or rather, service unit files, have the same base structure with some customizations as required.
All systemd service files must have three sections:[Service],[Unit], and[Install], and a few parameters under each section. Here’s what each section contains and why they are important:

TheUnitsection includes important metadata like a description and dependencies of the service. It has three parameters:Description,Before, andAfter. As the same suggests, theDescriptionparameter provides some context of the service and what it does.
TheBeforeandAfterparameters define the conditions to be met for the service to execute. For example, if you are starting a web server service, you would want it to start only after the network service is online. So you would set the value of theAfterparameter to the network service.

2. Service
TheServicesection contains two mandatory parameters:ExecStart,Type, and a few other optional parameters likeExecReload, and more.
ExecStartdefines the command to be executed when the service starts whereas theTypeparameter determines the type of process to spawn.

3. Install
This section and its data are called whenever you enable or disable the service withthe systemctl command.
It has a couple of parameters. One of the common and necessary ones isWantedBy. TheWantedByparameter defines the target units that should start whenever the service is enabled. The default value ismulti-user.target.
The parameters mentioned here are not the only parameters you can set in a unit file. You can get the full list of parameters from theofficial systemd.exec documentationor by typing inman systemd.execin a terminal.
A systemd service unit file always ends with a “.service” extension and must be stored in the/etc/systemd/system/directory. Create the service fileusing the touch commandwith elevated privileges by prefixing it with thesudocommand:
Now that you’ve created the service file, let’s get to populating it with the necessary syntax to make the service valid and working.
Step 2: Configuring the Service File
For demonstration, you’ll be creating a sample service that executesNmap to scan the ports on your machineand store the output in a file every thirty seconds. To achieve this task, here’s how to structure the service unit file:
Although the parameters mentioned here have been explained previously, let’s go over how they affect our newly created service and also explore new ones introduced in this unit:Restart,RestartSec, andUser.
Here’s what the parameters in each section mean:
Step 3: Enabling and Starting the Service
Now that you’ve created the unit file, the only steps left to activate your service are enabling and running it. You can enable and start your service using the systemctl command.
Here’s how to use systemctl to enable, start, and check the status of your service:
Now, your custom service should be up and running! In this case, you should see a Nmap scan running every thirty seconds and the output getting stored in theresults.txtfile in the home directory.
Setting Up a systemd Service on Linux Is Easy!
While setting up a custom service from scratch may seem daunting at first, it’s quite an easy way to set up a self-executing, unattended process to carry out repetitive tasks for you and boost your productivity.
In the demo service, we’ve barely scratched the surface of how you can configure and set up a service. To learn all about how you can customize your service, it’s recommended that you go through the official documentation linked in the note.
If setting up and maintaining a service feels like too much of a task to you, consider checking out an alternative way to automate repetitive tasks on Linux.
Play smart and set up phrases and scripts with AutoKey to automate basic tasks on a Linux machine.
Anyone with more than a passing interest in motorsports must see these films.
You’ve been quoting these famous films wrong all along!
Free AI tools are legitimately powerful; you just need to know how to stack them.
OneDrive is one of the best, but it has a catch.
Unlock a world of entertainment possibilities with this clever TV hack.