Introduction
Systemd is a system and service manager for Linux operating systems that introduces the concept of systemd units to manage different resources of the system. Creating a custom systemd service in Ubuntu enables users to start, stop, and manage custom background processes effectively. This tutorial covers the process of creating a custom systemd service on Ubuntu, from basic to advanced use cases.
Understanding systemd
Before jumping into creating a custom service, it’s essential to understand what systemd is and how it works. systemd provides a standard process for managing what programs run when a Linux system boots up and supervises them throughout the system’s lifecycle.
Basic Components of systemd
- Unit file: This is a configuration file for systemd that describes a service, a mount point, a socket, etc.
- Service unit: A specific type of unit file that describes how to manage a service or application.
Creating a Basic systemd Service
Let’s start by creating a simple systemd service.
Step 1: Write a Script
First, create a script that you want to manage as a service. Let’s say /usr/local/bin/my_script.sh
. Make sure it’s executable:
#!/bin/bash
# My custom script
echo "Hello, World!"
Make the script executable with:
chmod +x /usr/local/bin/my_script.sh
Step 2: Create a Service Unit File
Create a new unit file at /etc/systemd/system/my_service.service
with the following content:
[Unit]
Description=My Custom Service
[Service]
Type=simple
ExecStart=/usr/local/bin/my_script.sh
[Install]
WantedBy=multi-user.target
Here, we specify our script as the ExecStart
command, which systemd will call when starting the service.
Step 3: Manage the Service
To enable and start the service, use the following commands:
systemctl daemon-reload
systemctl enable my_service
systemctl start my_service
Check the status of the service with:
systemctl status my_service
The output will show you the status of your new systemd service.
Managing Service Execution
In this section, we’ll discuss how to manage the environment and user privileges for your service.
Setting Environment Variables
You can set environmental variables directly in your service file:
[Service]
Environment="VAR1=value1" "VAR2=value2"
ExecStart=/usr/local/bin/my_script.sh
Running as a Non-root User
To run the service as a non-root user, specify this in the service file:
[Service]
User=myuser
Group=mygroup
ExecStart=/usr/local/bin/my_script.sh
Advanced Usage
Let’s dive into some advanced configuration options.
Setting Up a Timer
To execute a service based on a timer, first create a timer unit my_service.timer
with content like:
[Unit]
Description=Runs my_service every hour
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
Then, enable and start the timer:
systemctl enable my_service.timer
systemctl start my_service.timer
Restart Policies
In your service file, you can define behavior if the service exits unexpectedly:
[Service]
Type=simple
ExecStart=/usr/local/bin/my_script.sh
Restart=on-failure
RestartSec=5s
This will restart your service after 5 seconds if it fails.
Securing Services
Security is a crucial aspect, especially for services exposed to the network. systemd provides options to tighten up security.
PrivateTmp
Isolates /tmp and /var/tmp directories for the service:
[Service]
PrivateTmp=true
ExecStart=/usr/local/bin/my_script.sh
System Call Filtering
Filter what system calls the service can execute:
[Service]
SystemCallFilter= @system-service
ExecStart=/usr/local/bin/my_script.sh
Troubleshooting and Maintenance
As with any system component, you may encounter issues that need troubleshooting.
Viewing Logs
Use journalctl to view service logs:
journalctl -u my_service.service
Reloading the Service
To apply changes without restarting:
systemctl daemon-reload
systemctl restart my_service
Conclusion
Creating a custom systemd service on Ubuntu can help you manage applications and processes effectively. Systemd’s extensive features offer fine control over service behavior, allowing for automated and secure management throughout your system’s lifecycle.