Think of Systemd Service as the brain behind how your Linux system runs. It ensures that when you turn on your computer, everything starts in the right order and keeps running smoothly.
Systemd manages different kind of services, which are ultimately the programs or processes that runs in the background.
Examples of services include:
For Example: An Nginx web server running on port 443 and serving traffic. Now Suppose if the server got rebooted due to some underlying OS related issues, but when it comes up, we eventaully want our service also to be UP and Running without any manual intervnetion. Hence, for this we can run a service as a systemd process, so that it eventually automatically comes UP.
This is one of the use cases for the Systemd.
Why Do We Need Systemd?
Startup Management:
When you press the power button, your computer doesn’t start automatically. Systemd makes sure things start in the correct order.
For example:
First, mount the disk so the system can read files.
Next, start the network services so you can connect to the internet.
Finally, launch the desktop environment or other user applications.
Keeps the System Running:
Just Imagine your nginx web server crashes, without Systemd, you might have to restart it manually.
With Systemd, we can set the services in such a way that it observes the crash and restarts the server automatically.
Easy Control:
Systemd provides simple commands to manage services. For example, you can start, stop, or restart a service using just one command.
How to create a Systemd Service?
Write the Service File
Service files define and tells how your application or script will be managed. They are typically stored in the path /etc/systemd/system/
.
- Open a terminal and create a service file:
sudo vim /etc/systemd/system/my-custom-service.service
- Populate the file with the following structure:
[Unit]
Description=<Add Description for your service>
After=network.target
[Service]
ExecStart=</path/to/your/script>
Restart=on-failure
User=<USER>
Group=<GROUP>
Environment="ENV_VAR_NAME=value"Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target - Now, Reload Systemd and Start the Service.
Once the service file is created, reload Systemd to recognize it:sudo systemctl daemon-reload
- Start the service:
sudo systemctl start my-custom-service
- To Enable it to run at boot:
sudo systemctl enable my-custom-service
- To disable your service on every reboot
sudo systemctl disable
my-custom-service
- To check the status of your service.
sudo systemctl staus
my-custom-service
Common Flags:
ExecStart
: This is the command which will be used to execute your custom script.Restart
: This flag defines the restart policies. Common values are:no
: Do not restart.on-failure
: Restart if the service fails.always
: Restart no matter the exit code.User
: It defines the user which will be used to execute this script. It is recommended to create a dedicated custom system user with appropriate permissions.Group:
It is used to define the group under which the user is added.Environment
: This is used to pass any custom ENV variables to the script.
The above configuration file defines a service that:
- Starts after the network is ready.
- Runs a program defined at
ExecStart
. - Automatically restarts if it fails and the time it needs to wait for restart is 3s.
- Runs as user-defined under <USER> and is started during the multi-user target (normal boot).
How They Work Together
In the Service file, there are multiple parameters supported like:
- [Unit]: specifies the what and when. It provides metadata and dependencies which are needed to successfully run this service.
- [Service]: specifies the how. It defines the behavior and the commands like what it should run, how it runs, and what happens if it fails.
- [Install]: specifies the where. It determines when and how the service is started during startup/boot.
🚩 Our Recent Posts
- How to keep 99.9% Uptime for application using Montioring & Alerting?
- How to Reduce/Save Costs on Microsoft Azure Log Analytics Workspace: Tips & Best Practices
- What is Grafana? How to Install and Set Up Grafana for Effective Data Visualization?
I’m a DevOps Engineer with 3 years of experience, passionate about building scalable and automated infrastructure. I write about Kubernetes, cloud automation, cost optimization, and DevOps tooling, aiming to simplify complex concepts with real-world insights. Outside of work, I enjoy exploring new DevOps tools, reading tech blogs, and play badminton.