Linux Service
systemd
is an essential skill for managing applications and processes. Here’s an overview of everything you need to know, from basic concepts to advanced usage.
What is systemd?
systemd
is a system and service manager for Linux that initializes the system and manages services. It uses unit files to define services, targets, and other system components.
Key Concepts
- Unit Files:
- The configuration files used by
systemd
. - Common types of unit files:
.service
: Describes a service..target
: Groups units into logical targets (e.g.,multi-user.target
)..timer
: Schedules tasks..mount
: Manages filesystem mounts.
- The configuration files used by
- Unit File Locations:
- System-wide:
/etc/systemd/system/
- Default:
/usr/lib/systemd/system/
- User-specific:
~/.config/systemd/user/
- System-wide:
- Service States:
active
: Running.inactive
: Stopped.failed
: Encountered an error.
Creating a Service
1. Writing a Service File
Example: my-service.service
[Unit]
Description=My Custom Service
After=network.target
[Service]
Type=simple
ExecStart=/path/to/your/application --arg1 --arg2
WorkingDirectory=/path/to/working/directory
User=myuser
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
2. Key Sections in a Service File
[Unit]
:- Describes the service and its dependencies.
Description
: Short description of the service.After
: Specifies when the service should start (e.g., afternetwork.target
).
[Service]
:- Describes how the service operates.
Type
: Defines the service type:simple
: (default) Starts the process specified inExecStart
.forking
: Used for daemons that fork.oneshot
: For one-time tasks.notify
ordbus
: For advanced use cases.
ExecStart
: Command to start the service.WorkingDirectory
: Sets the working directory.User
: User account under which the service runs.Restart
: Specifies restart behavior (always
,on-failure
, etc.).RestartSec
: Delay between restart attempts.
[Install]
:- Defines how and when the service is enabled.
WantedBy
: Defines the target in which the service is included (e.g.,multi-user.target
for most services).
3. Steps to Enable and Start a Service
Place the Service File:
sudo cp my-service.service /etc/systemd/system/
Reload systemd:
sudo systemctl daemon-reload
Enable the Service (Start at Boot):
sudo systemctl enable my-service.service
Start the Service:
sudo systemctl start my-service.service
Check Status:
sudo systemctl status my-service.service
Managing Services
- Basic Commands:
Start a service:
sudo systemctl start my-service
Stop a service:
sudo systemctl stop my-service
Restart a service:
sudo systemctl restart my-service
Reload service configuration without restarting:
sudo systemctl reload my-service
Check service status:
sudo systemctl status my-service
- Enable/Disable Services:
Enable a service (start at boot):
sudo systemctl enable my-service
Disable a service:
sudo systemctl disable my-service
- Inspect Logs:
View logs for a specific service:
journalctl -u my-service
Tail logs in real-time:
journalctl -u my-service -f
- Remove a Service:
Stop the service:
sudo systemctl stop my-service
Disable the service:
sudo systemctl disable my-service
Delete the service file:
sudo rm /etc/systemd/system/my-service.service
Reload systemd:
sudo systemctl daemon-reload
Advanced Topics
1. Environment Variables
You can pass environment variables to a service using Environment=
or EnvironmentFile=
.
Example:
Environment=MY_VAR=value
EnvironmentFile=/path/to/env/file
2. Dependency Management
Requires
: Specifies hard dependencies. If the required unit fails, this unit also fails.Wants
: Specifies soft dependencies. If the dependency fails, this unit still runs.Before
/After
: Specifies the order of startup.
Example:
[Unit]
Requires=network.target
After=network.target
3. Timer Units
Use .timer
units to schedule tasks, similar to cron
.
Example: my-service.timer
[Unit]
Description=Run My Service Every Day
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
4. User Services
Systemd also supports per-user services that don’t require root.
Place the service file in
~/.config/systemd/user/
.Reload user services:
systemctl --user daemon-reload
Enable and start the service:
systemctl --user enable my-service systemctl --user start my-service
5. Debugging Services
Check for syntax errors in the service file:
sudo systemd-analyze verify /etc/systemd/system/my-service.service
Debug logs:
sudo journalctl -xe
Test the service manually:
sudo -u myuser /path/to/application --arg1 --arg2
Best Practices
Use Absolute Paths: Systemd requires absolute paths for all file and directory references.
Limit Privileges: Run services as non-root users wherever possible.
Enable Restart Policies: Use
Restart=always
orRestart=on-failure
for critical services.Use
systemctl edit
for Overrides: To modify system services without editing the main service file:sudo systemctl edit my-service
This creates a drop-in configuration in
/etc/systemd/system/my-service.d/
.