Creating a Linux Service with systemd

Creating a Linux Service with systemd

When you think of automation on Linux, creating a service with systemd is like hiring a butler who works tirelessly behind the scenes. Whether you’re setting up a custom script to run on startup or managing a critical application, understanding systemd services can make your life as a Linux user a whole lot easier.


TL;DR

Learn how to create a custom Linux service using systemd by defining a unit file, enabling it to run at startup, and troubleshooting common issues. This guide walks you through every step with practical examples and tips.


Why systemd Services Matter

Imagine you’ve written a script to back up important files every day. You could run it manually—but who has time for that? Creating a service ensures your script runs automatically and reliably. With systemd, you can manage when and how services start, stop, and restart, making it a powerful tool for automation.


Step 1: Understand systemd Unit Files

A systemd service is defined by a unit file, which is essentially a configuration file. These files are stored in:

  • /etc/systemd/system/ for system-wide services.
  • ~/.config/systemd/user/ for user-specific services.

Unit files use a structured format with sections like [Unit], [Service], and [Install] to define the service’s behavior.


Step 2: Create a Custom Script

First, let’s create a simple script. Save the following example as /usr/local/bin/my_custom_script.sh:

  1. Make the script executable:
  1. Test the script:

Check the log file at /var/log/my_custom_service.log to verify it works.


    Step 3: Define the Service

    Create a new unit file for your service:

    1. Open a text editor to create /etc/systemd/system/my_custom_service.service:
    1. Add the following content:

    Here’s what each section does:

    • [Unit]: Describes the service and its dependencies.
    • [Service]: Specifies the command to run and its behavior.
    • [Install]: Defines when the service should start (e.g., at boot).

    Step 4: Enable and Start the Service

    1. Reload systemd to recognize the new service: sudo systemctl daemon-reload
    2. Enable the service to start at boot: sudo systemctl enable my_custom_service
    3. Start the service: sudo systemctl start my_custom_service
    4. Verify the service is running: sudo systemctl status my_custom_service

    Step 5: Troubleshooting Common Issues

    If your service doesn’t work as expected, try these tips:

    • Check the logs: sudo journalctl -u my_custom_service
    • Test the script manually to ensure it runs without errors.
    • Validate the unit file for syntax errors: systemd-analyze verify /etc/systemd/system/my_custom_service.service
    • Restart the service after making changes: sudo systemctl restart my_custom_service

    Best Practices

    1. Use Absolute Paths: Always use full paths for scripts and commands in the ExecStart directive.
    2. Set Permissions: Ensure scripts and log files have proper permissions to avoid access issues.
    3. Enable Restart Policies: Use Restart=on-failure or similar options to make services resilient.
    4. Keep Logs Organized: Use a dedicated log directory, such as /var/log/my_service/.

    References

    Leave a Reply

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