How to Create a Systemd Service in Linux

This comprehensive guide focuses on mastering systemd services—an integral aspect of contemporary Linux systems. By delving into the structure of systemd service files and understanding how to create custom services for root and normal users, readers will acquire the knowledge and expertise necessary to manage and develop systemd services proficiently. Without further ado, let the exploration begin.

What is a systemd service?

Systemd is a powerful init system and service manager for Linux operating systems, created to streamline and simplify the management of various system components and services. With its versatile functionality, systemd can start and stop services, establish dependencies between services, and efficiently manage the boot process. A systemd service constitutes a unit configuration file that delineates how a specific service will be supervised and controlled by the systemd system. These services play a crucial role in maintaining the stability and performance of Linux systems.

Understanding the basic structure of a systemd service file

A systemd service file is a structured plain text file that houses a collection of directives organized into distinct sections. Each section commences with a header enclosed in square brackets, such as [Unit], [Service], or [Install]. The directives within each section outline the service’s behavior, requirements, and configuration, ensuring smooth operation and seamless integration with the systemd system. By comprehending the structure and syntax of systemd service files, system administrators can efficiently create, modify, and troubleshoot services, thereby enhancing their Linux systems’ overall performance and reliability.

The [Unit] section

The [Unit] section contains general information and metadata about the service. This section typically includes directives like Description, After, and Requires, which provide a human-readable description, specify the ordering of services and define dependencies, respectively.

Example:

[Unit]
Description=My Custom Service
After=network.target
Requires=network.target

The [Service] section

The [Service] section is where the main configuration of the service takes place. This section includes directives that define the service’s behavior, such as Type, ExecStart, ExecStop, Restart, and TimeoutStartSec.

Example:

[Service]
Type=simple
ExecStart=/usr/bin/my_custom_service
ExecStop=/usr/bin/my_custom_service --stop
Restart=on-failure
TimeoutStartSec=30

The [Install] section

The [Install] section explains how the service should be installed and enabled. It typically includes directives like WantedBy, which defines the target that the service should be installed under.

Example:

[Install]
WantedBy=multi-user.target

Creating your own systemd service

To create your own systemd service, follow these steps:

  1. Create a new file with the .service extension in the /etc/systemd/system/ directory. For example, create a file named my_custom_service.service. You can use any text editor you prefer, like nano or vim. To create the file with nano, run:
sudo nano /etc/systemd/system/my_custom_service.service
  1. Define the [Unit], [Service], and [Install] sections as described earlier in this article. Here’s an example of a custom service for a Python script:
[Unit]
Description=My Python Script Service
After=network.target

[Service]
Type=simple
User=myuser
ExecStart=/usr/bin/python3 /home/myuser/my_script.py
Restart=on-failure

[Install]
WantedBy=multi-user.target

Another common example is a systemd service for a web server like Nginx:

[Unit]
Description=My Nginx Web Server
After=network.target

[Service]
Type=forking
User=root
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
Restart=always

[Install]
WantedBy=multi-user.target
  1. Save the file and exit the editor. If you used nano, press Ctrl + X, then press Y to confirm saving the changes, and finally press Enter.
  1. Run the following command to reload the systemd daemon and make it aware of your new service:
sudo systemctl daemon-reload
  1. Enable and start the service using the following commands:
sudo systemctl enable my_custom_service
sudo systemctl start my_custom_service

Systemd service for root user

Creating a systemd service for the root user is similar to creating a regular service. The main difference is that the service will run with root privileges. This can be achieved by using the User=root directive in the [Service] section.

Example:

[Service]
User=root
ExecStart=/usr/bin/my_custom_service

Systemd service for a normal user

To create a systemd service for a normal user, use the User=username directive in the [Service] section, where username is the name of the non-root user you want the service to run as.

Example:

[Service]
User=johndoe
ExecStart=/usr/bin/my_custom_service

Conclusion

This article has covered the fundamentals of systemd services, their structure, and how to create custom services for both root and normal users. By understanding and mastering systemd services, you can effectively manage and maintain various services running on your Linux systems.

Additional resources and relevant links

  • Systemd official documentation: The official documentation for systemd provides a comprehensive reference for various systemd components, including unit files, unit commands, and service management. This is an essential resource for anyone looking to understand the intricacies of systemd and its various features.
  • The systemd project on GitHub: This is the official GitHub repository for the systemd project. It contains the source code, bug reports, and feature requests, as well as an extensive README file with information about the project.

Your Mastodon Instance
Share to...