Create Systemd Service in Linux: Master systemd, Auto-Restart & Manage with systemctl

systemd services

Linux is famous for its flexibility and stability, but if you’ve ever deployed something like Prometheus or Grafana, you might have noticed one issue: the process disappears after logging out or restarting the server. Suddenly, your monitoring stack is gone, logs are empty, and panic begins.

That’s where systemd comes into play. It’s the process manager that ensures your applications keep running in the background even after reboots or crashes. In this guide, you’ll learn how to create systemd service in Linux, run applications at boot, auto-restart them on failure, and manage everything easily with systemctl commands.

Prefer watching instead? Check out my YouTube video on mastering systemd

What is systemd?

Think of systemd as the captain of a Linux system. As soon as the kernel boots, systemd takes charge and begins managing processes.

Its key responsibilities include:

  • Starting services automatically
  • Managing logs
  • Monitoring processes for crashes
  • Restarting services if they fail

In short, systemd is the boss of all services in a Linux environment.

Exploring Existing systemd Services

Before creating a custom service, let’s explore what’s already running. Open a terminal and type:

systemctl list-units --type=service

This lists all services currently managed by systemd. To check the details of a specific service, for example, SSH:

systemctl status sshd

You’ll see:

  1. Loaded → systemd recognizes the service.
  2. Active → shows whether it’s running or stopped.
  3. Main PID → process ID.
  4. Recent logs → great for debugging issues.

This command alone provides almost everything needed to understand the state of a Linux service.

Creating Your First systemd Service

Let’s build a simple service that logs the current time every 10 seconds.

Step 1: Create the Script

First, create a directory for your custom scripts:

sudo mkdir -p /opt/custom-services

Now create the script:

sudo vi /opt/custom-services/log-time.sh

Paste this inside:

#!/bin/bash
while true; do
  echo "$(date) - I am alive" >> /var/log/time.log
  sleep 10
done

Make it executable:

sudo chmod +x /opt/custom-services/log-time.sh
Step 2: Create the systemd Unit File

Now tell systemd how to run this script. Create a service file:

sudo vi /etc/systemd/system/logtime.service

Paste this configuration:

[Unit]
Description=Log Current Time Every 10 Seconds
After=network.target

[Service]
ExecStart=/opt/custom-services/log-time.sh
Restart=always

[Install]
WantedBy=multi-user.target

Explanation:
  • Description → a friendly name for the service.
  • After=network.target → ensures the service starts after networking is up.
  • ExecStart → path to the script.
  • Restart=always → service restarts automatically if it crashes.
  • WantedBy=multi-user.target → runs automatically at system boot.

Testing the Service

Reload systemd and start the new service:

sudo systemctl daemon-reload
sudo systemctl enable --now logtime.service

Check its status:

systemctl status logtime.service

And verify the log file:

tail -f /var/log/time.log

If everything works, you’ll see a new line every 10 seconds confirming that the service is alive.

Bonus: Restart, Logs, and Troubleshooting

Automatic Restart

Kill the service process and watch it restart automatically:

ps aux | grep log-time.sh
sudo kill <pid>
systemctl status logtime.service
View Logs with journalctl

Systemd keeps detailed logs for each service:

journalctl -u logtime.service -f

This is extremely useful when debugging services.

Quick Troubleshooting Tips

  • Always run sudo systemctl daemon-reload after modifying a unit file.
  • Check service status with systemctl status <service-name>.
  • Use journalctl -u <service-name> to view logs.

Why systemd Matters in Real Projects

Custom services are not just for fun scripts. In real-world setups, tools like Prometheus, Grafana, and Node Exporter are often run as systemd services. This ensures:

  • Applications start automatically at boot.
  • Services survive server restarts.
  • Processes recover from crashes instantly.

For DevOps engineers and Linux administrators, mastering systemd is a must-have skill.

Wrap Up

You’ve just learned how to:

  • Explore existing systemd services
  • Write a custom script
  • Create a systemd unit file
  • Enable, start, and monitor a service
  • Troubleshoot using journalctl

By mastering systemd, you’ll ensure your applications are resilient, reliable, and production-ready.

Further Reading

If you’d like to explore more about systemd in detail, check out these resources:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top