How to create a service for Fedora/RHEL

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.

Requirements

  • Fedora/RHEL distro family flavor

Create the background service file

[Unit]
Description=my-service nginx example
After=network.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=default.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.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.