Skip to main content

Command Palette

Search for a command to run...

How to Create Your First Linux Service in 10 Minutes: A Complete Beginner's Guide

Stop wondering how background processes work on Linux. Build your own custom service from scratch and unlock the power of automation

Published
โ€ข8 min read
How to Create Your First Linux Service in 10 Minutes: A Complete Beginner's Guide
K
DevOps Engineer with hands-on experience in designing, deploying, and operating scalable, secure, and highly available AWS cloud-native solutions. Strong expertise in CI/CD automation, containerization, Infrastructure as Code, cloud networking, security, and observability. Focused on immutable infrastructure, continuous delivery, and building low-maintenance platforms for global software systems.

๐ŸŽฏ What You'll Learn

By the end of this tutorial, you'll have:

  • โœ… Deep understanding of Linux services and systemd

  • โœ… A working custom service that you built from scratch

  • โœ… Skills to create any automation service for your projects

  • โœ… Real-world Linux administration experience

  • โœ… Troubleshooting skills for service management


๐Ÿ” What Exactly is a Linux Service?

A Linux service (also called a daemon) is a background program that:

  • Runs continuously without user interaction

  • Starts automatically when the system boots

  • Can be controlled with simple commands (start, stop, status)

  • Handles system tasks like logging, networking, or custom automation

Think of services as the "invisible workers" of your Linux system - they're always running in the background, keeping things operational.

Real-world examples:

  • nginx - Web server service

  • mysql - Database service

  • ssh - Remote access service

  • cron - Task scheduler service


๐Ÿ—๏ธ Project Overview: Timer Service

We're going to build a service called timer that:

  • Writes the current timestamp to a file every 5 seconds

  • Can be started, stopped, and monitored like any professional service

  • Demonstrates all the core concepts of Linux service management

Why this project? It's simple enough to understand but comprehensive enough to teach you real systemd skills you'll use in production environments.


๐Ÿ› ๏ธ Prerequisites (Super Simple!)

  • Linux Environment: I used iximiuz Labs playground (https://labs.iximiuz.com/playgrounds) - it's free and gives you instant access to real Ubuntu machines!

  • 5-10 Minutes: That's honestly all you need

  • Zero Experience Required: I'll explain every single step

๐Ÿ’ก Why iximiuz Labs? No downloads, no setup, no VM configuration headaches. Just click and you have a real Linux terminal ready to go!


๐Ÿ“‹ Step-by-Step Implementation

Step 1: Get Your Playground Ready

Instead of dealing with WSL setup headaches, I used iximiuz Labs (https://labs.iximiuz.com/playgrounds). Here's why it's perfect for learning:

  • โœ… Instant Access - No downloads or installations

  • โœ… Real Ubuntu Environment - Not a simulation

  • โœ… Free to Use - Perfect for learning and experimentation

  • โœ… Pre-configured - Everything you need is already there

Just visit the site, start an Ubuntu playground, and you'll see:

bash

laborant@ubuntu-01:~$

That's your real Linux terminal, ready to go!

Step 2: Create the Service Script

This script will do the actual work - writing timestamps to a file:

Note: If nano isn't available (like in my case), use vi instead:

bash

sudo vi /usr/local/bin/timer-script.sh

Content of timer-script.sh:

bash

#!/bin/bash

# Define the output file in user's home directory
OUTPUT_FILE="$HOME/timer-output.txt"

# Function to handle cleanup on exit
cleanup() {
    echo "Timer service stopped at $(date)" >> "$OUTPUT_FILE"
    exit 0
}

# Trap signals to ensure clean exit
trap cleanup SIGTERM SIGINT

# Write initial message
echo "Timer service started at $(date)" >> "$OUTPUT_FILE"

# Main loop - write timestamp every 5 seconds
while true; do
    echo "Current time: $(date)" >> "$OUTPUT_FILE"
    sleep 5
done

Make it executable:

bash

sudo chmod +x /usr/local/bin/timer-script.sh

Step 3: Create the systemd Service File

This is where the magic happens - we tell systemd how to manage our service:

bash

sudo nano /etc/systemd/system/timer.service

Content of timer.service:

ini

[Unit]
Description=Timer Service - Writes timestamps every 5 seconds
Documentation=https://your-blog-link.com
After=network.target

[Service]
Type=simple
User=YOUR_USERNAME
ExecStart=/usr/local/bin/timer-script.sh
Restart=always
RestartSec=3
Environment=HOME=/home/YOUR_USERNAME

[Install]
WantedBy=multi-user.target

โš ๏ธ Important: Replace YOUR_USERNAME with your actual username:

bash

# Find your username
whoami

# Example result: laborant
# So you'd use: User=laborant and Environment=HOME=/home/laborant

After troubleshooting and fixing the service file:

bash

# Stop the failing service first
sudo systemctl stop timer

# Edit the service file (remove Group= line)
sudo vi /etc/systemd/system/timer.service

# Reload systemd
sudo systemctl daemon-reload

# Start the service
sudo systemctl start timer

# Verify it's working
sudo systemctl status timer

Step 5: Test and Verify

Check service status:

bash

sudo systemctl status timer

You should see something like:

โ— timer.service - Timer Service - Writes timestamps every 5 seconds
   Loaded: loaded (/etc/systemd/system/timer.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2025-08-10 12:30:15 UTC; 1min 23s ago

View the output file:

bash

# Check if file exists
ls -la ~/timer-output.txt

# View contents
cat ~/timer-output.txt

# Watch in real-time
tail -f ~/timer-output.txt

Stop the service:

bash

sudo systemctl stop timer

๐Ÿ”ง Troubleshooting Common Issues

Issue 1: "Failed to determine group credentials" (Most Common!)

This is exactly what I encountered! The error looked like this:

timer.service: Failed to determine group credentials: No such process
systemd[1]: timer.service: Main process exited, code=exited, status=216/GROUP

Root Cause: The service file had incorrect User= and Group= settings.

Solution:

bash

# First, check your actual username and group
whoami          # Result: laborant
id -gn          # Result: laborant
echo $HOME      # Result: /home/laborant

# Then edit the service file with YOUR actual details
sudo vi /etc/systemd/system/timer.service

Key Fix: Make sure your service file looks like this (replace with YOUR username):

ini

[Unit]
Description=Timer Service - Writes timestamps every 5 seconds
After=network.target

[Service]
Type=simple
User=user_name
ExecStart=/usr/local/bin/timer-script.sh
Restart=always
RestartSec=3
Environment=HOME=/home/USER_NAME

[Install]
WantedBy=multi-user.target

Important: I removed the Group= line entirely - this often fixes the credentials issue!

Issue 2: Permission denied on script

Solution:

bash

# Make script executable
sudo chmod +x /usr/local/bin/timer-script.sh

# Check permissions
ls -la /usr/local/bin/timer-script.sh

Issue 3: Service won't start

Check logs for detailed error messages:

bash

sudo journalctl -u timer -f

My Real Experience: I spent time troubleshooting the group credentials error (restart counter went up to 89!), but once I fixed the service file and reloaded systemd, everything worked perfectly. The service started writing timestamps every 5 seconds exactly as expected.

Final verification showed:

bash

โ— timer.service - Timer Service - Writes timestamps every 5 seconds
     Loaded: loaded (/etc/systemd/system/timer.service; enabled; preset: enabled)
     Active: active (running) since Sun 2025-08-10 12:49:25 UTC; 7s ago
   Main PID: 1677 (timer-script.sh)
      Tasks: 2 (limit: 9535)
     Memory: 720.0K ()
        CPU: 12ms

The output file was successfully created:

bash

laborant@ubuntu-01:~$ ls -la /home/laborant/timer-output.txt
-rw-r--r-- 1 laborant laborant 559 Aug 10 12:50 /home/laborant/timer-output.txt

๐ŸŽจ User-Friendly Alternative: User Service

For a simpler approach that doesn't require sudo rights:

bash

# Create user service directory
mkdir -p ~/.config/systemd/user

# Create service file
nano ~/.config/systemd/user/timer.service

User service content:

ini

[Unit]
Description=User Timer Service
After=default.target

[Service]
Type=simple
ExecStart=/bin/bash -c 'while true; do echo "Current time: $(date)" >> %h/timer-output.txt; sleep 5; done'
Restart=always
RestartSec=3

[Install]
WantedBy=default.target

Manage user service:

bash

# Start
systemctl --user daemon-reload
systemctl --user start timer

# Status
systemctl --user status timer

# Stop  
systemctl --user stop timer

๐Ÿ“Š Understanding the Service File Structure

Let's break down what each section does:

ini

[Unit]
Description=Timer Service - Writes timestamps every 5 seconds
After=network.target
  • Description: Human-readable description

  • After: Wait for network before starting

ini

[Service]
Type=simple
User=USER_NAME
ExecStart=/usr/local/bin/timer-script.sh
Restart=always
RestartSec=3
  • Type=simple: Service runs in foreground

  • User: Which user to run as

  • ExecStart: Command to execute

  • Restart=always: Auto-restart if it crashes

  • RestartSec=3: Wait 3 seconds before restarting

ini

[Install]
WantedBy=multi-user.target
  • WantedBy: When to start this service (multi-user mode)

๐Ÿš€ Advanced: Real-World Applications

Now that you understand the basics, here are some practical services you can create:

1. Log Monitor Service

Monitor log files and send alerts:

bash

ExecStart=/bin/bash -c 'tail -f /var/log/syslog | grep ERROR >> /home/user/error-alerts.txt'

2. Backup Automation Service

Automated file backups:

bash

ExecStart=/home/user/scripts/backup-script.sh

3. System Health Monitor

Check disk space, memory, CPU:

bash

ExecStart=/home/user/scripts/health-monitor.sh

4. API Health Checker

Monitor website/API availability:

bash

ExecStart=/bin/bash -c 'while true; do curl -s https://api.example.com/health >> /home/user/api-status.log; sleep 60; done'

๐Ÿ“ˆ Performance and Best Practices

Security Best Practices:

  • โœ… Always run services as non-root users when possible

  • โœ… Use specific user accounts for different services

  • โœ… Set proper file permissions (755 for scripts, 644 for service files)

  • โœ… Validate input in your scripts

Performance Tips:

  • โœ… Use appropriate sleep intervals (don't overwhelm the system)

  • โœ… Implement log rotation to prevent disk space issues

  • โœ… Use efficient commands (avoid heavy operations in loops)

  • โœ… Monitor resource usage with htop or systemctl status

Maintenance:

  • โœ… Use journalctl to monitor service logs

  • โœ… Set up log rotation with logrotate

  • โœ… Test services thoroughly before production deployment

  • โœ… Document your services for team members


๐ŸŽฏ Key Commands Reference Card

Service Management:

bash

sudo systemctl start timer        # Start service
sudo systemctl stop timer         # Stop service  
sudo systemctl restart timer      # Restart service
sudo systemctl status timer       # Check status
sudo systemctl enable timer       # Auto-start on boot
sudo systemctl disable timer      # Don't auto-start

Debugging:

bash

sudo journalctl -u timer -f       # View live logs
sudo journalctl -u timer --since today  # Today's logs
systemctl --failed                # List failed services

User Services (no sudo needed):

bash

systemctl --user start timer
systemctl --user status timer
systemctl --user stop timer

๐Ÿ† What You've Accomplished

Congratulations! You've just:

  1. Created your first Linux service - A fundamental DevOps skill

  2. Learned systemd basics - The heart of modern Linux systems

  3. Mastered service debugging - Essential for production environments

  4. Built reusable automation - Perfect for monitoring and maintenance tasks

  5. Gained practical Linux experience - Real skills you'll use daily


๐Ÿ”— Next Steps

Ready to level up? Try these challenges:

  1. Modify the timer interval - Make it write every 10 seconds

  2. Add error handling - What happens if the disk is full?

  3. Create a web service - Build a simple HTTP server service

  4. Add configuration files - Make your service configurable

  5. Implement service dependencies - Make services depend on each other


๐Ÿ“š Additional Resources


๐Ÿ’ฌ Join the Discussion

Did this tutorial help you? Have questions or improvements?

Leave a comment below and share:

  • What services are you planning to create?

  • Any challenges you faced?

  • How you'll use this in your projects!

Follow me for more Linux tutorials and practical DevOps content!


Tags: #Linux #DevOps #SystemAdministration #WSL #Ubuntu #Systemd #Automation #BeginnerFriendly #Tutorial


Found this helpful? Share it with your fellow developers and give it a โค๏ธ!