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
:
#!/bin/bash
# My Custom Script
echo "Hello, systemd!" >> /var/log/my_custom_service.log
- Make the script executable:
sudo chmod +x /usr/local/bin/my_custom_script.sh
- Test the script:
/usr/local/bin/my_custom_script.sh
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:
- Open a text editor to create
/etc/systemd/system/my_custom_service.service
:
sudo nano /etc/systemd/system/my_custom_service.service
- Add the following content:
[Unit]
Description=My Custom Service
After=network.target
[Service]
ExecStart=/usr/local/bin/my_custom_script.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
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
- Reload systemd to recognize the new service:
sudo systemctl daemon-reload
- Enable the service to start at boot:
sudo systemctl enable my_custom_service
- Start the service:
sudo systemctl start my_custom_service
- 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
- Use Absolute Paths: Always use full paths for scripts and commands in the
ExecStart
directive. - Set Permissions: Ensure scripts and log files have proper permissions to avoid access issues.
- Enable Restart Policies: Use
Restart=on-failure
or similar options to make services resilient. - Keep Logs Organized: Use a dedicated log directory, such as
/var/log/my_service/
.