User services location
User service files are stored in:~/.config/systemd/user/.
Service file structure
Systemd service files use an INI-like format with sections and key-value pairs.Example: kanata.service
.config/systemd/user/kanata.service
Section breakdown
[Unit] section
[Unit] section
Contains metadata and dependencies:
- Description: Human-readable description of the service
- Documentation: URL to documentation or man pages
- After: Start this service after specified units
- Requires: Dependencies that must be running
[Service] section
[Service] section
Defines how the service runs:
- Type: Service type (
simple,forking,oneshot, etc.) - ExecStart: Command to start the service
- ExecStop: Command to stop the service (optional)
- Restart: Restart policy (
no,always,on-failure) - Environment: Environment variables for the service
[Install] section
[Install] section
Controls how the service is enabled:
- WantedBy: Target that should include this service when enabled
- RequiredBy: Stronger dependency relationship
Managing user services
Basic commands
All systemd user commands use the--user flag:
User services start when you log in and stop when you log out, unless you enable lingering (see below).
Enable lingering
To keep user services running after logout:- Start at boot
- Continue running after logout
- Survive session closures
Service lifecycle
Service types
Simple
The default type. Systemd considers the service started immediately after forking the ExecStart process.- Run in the foreground
- Don’t fork to background
- Keep running continuously
Oneshot
For services that run a single task and exit:- Setup scripts
- One-time initialization
- Scripts that should run on login
Forking
For traditional daemons that fork to background:Environment variables
You can set environment variables for services:Restart policies
Troubleshooting
Service fails to start
Test the ExecStart command
Run the command from ExecStart directly in your shell to see if it works.
Service doesn’t start on login
- Verify the service is enabled:
systemctl --user is-enabled kanata.service - Check if the service is masked:
systemctl --user list-unit-files | grep kanata - Ensure
WantedBy=default.targetis in the[Install]section
Changes not taking effect
Always reload systemd after editing service files:Listing services
Creating custom services
Template for a custom service
~/.config/systemd/user/my-service.service
Related resources
Kanata keyboard remapping
Configure Kanata with systemd
systemd.service man page
Official systemd service documentation