How to create a service for Fedora/RHEL (systemd)

server May 1, 2022

A common task for Linux administrators is to create a background task that must be executed in the background. The most popular service manager in the Linux world is the systemd. This is an example of how to create a new service and use it.

If you want to learn more about the systemd, I recommend this video made by "DJ Ware":

Requirements

  • Fedora/RHEL distro family flavor

Create the background service file

[Unit]
Description=my-service nginx example

# You may want to start after your network is ready
After=network-online.target
Wants=network-online.target

[Service]
ExecStart=/usr/bin/env docker run --name nginx --rm -p 8080:80 docker.io/nginx:alpine
Restart=Always
PIDFile=/tmp/my_service_pid

[Install]
WantedBy=network-online.target
/etc/systemd/system/my-service.service
# Create the service file
vim /etc/systemd/system/my-service.service

# (if you change an existent file, you must reload the daemon config)
# Reload daemon config
systemctl daemon-reload

# Enable
systemctl enable my-service.service

# Start
systemctl start my-service.service

# (if you need to restart)
# Restart
systemctl restart my-service.service
how to create a systemd service

References

Tags

Luiz Felipe F M Costa

I am a quality engineer at Red Hat / Ansible. I love automation tools, games, and coffee. I am also an active contributor to open-source projects on GitHub.