DEV Community

Cover image for Linux Services Made Simple: Create, Control, and Remove Your First systemd Service
Ubayed Bin Sufian
Ubayed Bin Sufian

Posted on

Linux Services Made Simple: Create, Control, and Remove Your First systemd Service

Linux as a service (The simplest Way to Understand It)

  • A service is just a program that runs in the background
  • systemctl is the tool to control it
  • systemd is the system that manages services

Simple analogy

Think of a service like a car 🚗:

  • You can start it
  • You can stop it
  • You can check if it’s running

The systemd is like the engine that keeps the car running smoothly behind the scenes.

What you'll learn

By the end of this, you’ll be able to:

  • Create your own service
  • Start / stop it using systemctl
  • Enable / disable it at boot
  • Delete it cleanly (no leftover mess)

Step 1: Create a simple script

Create a file:

sudo nano ~/my_simple_service.sh
Enter fullscreen mode Exit fullscreen mode

Paste this content:

#!/bin/bash

while true
do
  echo "Hello! Service is running..."
  sleep 5
done
Enter fullscreen mode Exit fullscreen mode

Save and exit.

  • Ctrl + O --> Enter
  • Ctrl + X

Make it executable:

chmod +x ~/my_simple_service.sh
Enter fullscreen mode Exit fullscreen mode

Create a simple script

Step 2: Create a systemd service

Now we tell systemd how to run our script.

sudo nano /etc/systemd/system/my-simple.service
Enter fullscreen mode Exit fullscreen mode

Paste this:

[Unit]
Description=My Simple Demo Service

[Service]
ExecStart=/home/$USER/my-simple-service.sh
Restart=always

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

👉 Replace /home/your-username with your actual username
(Don’t use $USER here — systemd won’t expand it properly)

Step 3: Reload systemd and start the service

sudo systemctl daemon-reload
sudo systemctl start my-simple.service
Enter fullscreen mode Exit fullscreen mode

Step 4: Check status

sudo systemctl status my-simple.service
Enter fullscreen mode Exit fullscreen mode

You should see the service running.

Create a systemd service

Step 5: Stop the service

sudo systemctl stop my-simple.service
Enter fullscreen mode Exit fullscreen mode

stop the service

Step 6: Enable / Disable (auto-start on boot)

Enable:

sudo systemctl enable my-simple.service
Enter fullscreen mode Exit fullscreen mode

Disable:

sudo systemctl disable my-simple.service
Enter fullscreen mode Exit fullscreen mode

Step 7: View logs (while service is running)

sudo journalctl -u my-simple.service -f
Enter fullscreen mode Exit fullscreen mode

-f - follow (like tail -f)

You should see:

"Hello! Service is running..."
Enter fullscreen mode Exit fullscreen mode

…repeating every 5 seconds.

View logs while service is running

Stop the service: sudo systemctl stop my-simple.service

View logs again: sudo journalctl -u my-simple.service

You will only see past logs (no new updates).

view logs when service is not running

Step 8: Clean up (Delete everything properly)

[CAUTION: PLEASE BE CAREFUL WHEN DELETING THE SERVICE FILES SO WE DO NOT DELETE ANY SYSTEM FILES]

sudo systemctl stop my-simple.service
sudo systemctl disable my-simple.service
sudo rm /etc/systemd/system/my-simple.service
sudo systemctl daemon-reload
sudo rm ~/my_simple_service.sh
Enter fullscreen mode Exit fullscreen mode

Now your system is back to a clean state—no leftover services, no hidden issues.

You just created and managed your first Linux service from scratch 🎉

This is a small step—but it’s exactly how real-world services (like web servers and databases) are managed in production.

Flow Diagram

How Systemd works

Disclaimer: I have used GH copilot for writing the topics, Gemini Nano Banana for image generation and ChatGPT for the flow diagram and other concepts.

Top comments (0)