The service needs setcap on the Python binary to access Bluetooth. Added verification steps to troubleshooting section.
6.1 KiB
6.1 KiB
Systemd Service Installation
Installing Sensorpajen as a User Service
This allows sensorpajen to run automatically on boot as your user (no sudo required for management).
Installation Steps
1. Install the Service File
cd ~/sensorpajen
# Create user systemd directory if it doesn't exist
mkdir -p ~/.config/systemd/user/
# Copy service file
cp systemd/sensorpajen.service ~/.config/systemd/user/
# Reload systemd to recognize the new service
systemctl --user daemon-reload
2. Enable Lingering (Run Without Login)
This allows your user services to run even when you're not logged in:
# Enable lingering for your user
sudo loginctl enable-linger $USER
# Verify it's enabled
loginctl show-user $USER | grep Linger
# Should show: Linger=yes
3. Start and Enable the Service
# Start the service now
systemctl --user start sensorpajen
# Enable it to start on boot
systemctl --user enable sensorpajen
# Check status
systemctl --user status sensorpajen
Service Management Commands
# Start the service
systemctl --user start sensorpajen
# Stop the service
systemctl --user stop sensorpajen
# Restart the service
systemctl --user restart sensorpajen
# Check status
systemctl --user status sensorpajen
# View logs (all)
journalctl --user -u sensorpajen
# View logs (follow/tail)
journalctl --user -u sensorpajen -f
# View logs (last 100 lines)
journalctl --user -u sensorpajen -n 100
# View logs (since specific time)
journalctl --user -u sensorpajen --since "1 hour ago"
journalctl --user -u sensorpajen --since "2025-12-27 10:00"
# Enable service (start on boot)
systemctl --user enable sensorpajen
# Disable service (don't start on boot)
systemctl --user disable sensorpajen
Viewing Logs
The service logs to systemd journal. View them with:
# Live view (like tail -f)
journalctl --user -u sensorpajen -f
# With timestamps
journalctl --user -u sensorpajen -f -o short-iso
# Just today's logs
journalctl --user -u sensorpajen --since today
Updating the Service
After making changes to the code:
# Pull latest changes
cd ~/sensorpajen
git pull origin master
# Restart the service to apply changes
systemctl --user restart sensorpajen
# Check it started correctly
systemctl --user status sensorpajen
After editing sensorpajen.service:
# Copy updated service file
cp systemd/sensorpajen.service ~/.config/systemd/user/
# Reload systemd configuration
systemctl --user daemon-reload
# Restart the service
systemctl --user restart sensorpajen
After editing configuration files:
# Edit config
nano ~/sensorpajen/config/sensorpajen.env
# or
nano ~/sensorpajen/config/sensors.json
# Restart service to reload config
systemctl --user restart sensorpajen
Troubleshooting
Permission Denied Errors
If you see PermissionError: [Errno 1] Operation not permitted in the logs:
# Verify Bluetooth capabilities are set on Python binary
getcap ~/.local/share/virtualenvs/*/bin/python3.*
# If not set, apply capabilities (adjust path to your venv):
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f ~/sensorpajen/.venv/bin/python3)
# Verify it was set:
getcap $(readlink -f ~/sensorpajen/.venv/bin/python3)
# Should show: cap_net_admin,cap_net_raw+eip
# Restart the service
systemctl --user restart sensorpajen
Important: Capabilities must be set on the actual Python binary, not symlinks. Use readlink -f to resolve the real path.
Service Won't Start
# Check detailed status
systemctl --user status sensorpajen
# Check logs for errors
journalctl --user -u sensorpajen -n 50
# Test the command manually
cd ~/sensorpajen
source .venv/bin/activate
export $(cat config/sensorpajen.env | grep -v '^#' | xargs)
python -m sensorpajen.main
Bluetooth Permission Errors
Make sure capabilities are set on the Python binary:
getcap $(readlink -f ~/.venv/bin/python3)
# Should show: cap_net_raw,cap_net_admin+eip
# If not set:
sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f ~/sensorpajen/.venv/bin/python3)
Service Doesn't Start on Boot
# Check if service is enabled
systemctl --user is-enabled sensorpajen
# Should show: enabled
# Check if lingering is enabled
loginctl show-user $USER | grep Linger
# Should show: Linger=yes
# If not enabled:
systemctl --user enable sensorpajen
sudo loginctl enable-linger $USER
Environment Variables Not Loading
# Verify environment file exists and is readable
cat ~/sensorpajen/config/sensorpajen.env
# Check file permissions
ls -la ~/sensorpajen/config/sensorpajen.env
# Test loading manually
export $(cat ~/sensorpajen/config/sensorpajen.env | grep -v '^#' | xargs)
env | grep MQTT
Verifying Everything Works
After installation:
# 1. Check service is running
systemctl --user status sensorpajen
# 2. Check logs show sensor data
journalctl --user -u sensorpajen -f
# 3. Check MQTT messages are being published
mosquitto_sub -h 192.168.0.114 -u hasse -P casablanca -t "MiTemperature2/#" -v
# 4. Reboot and verify it starts automatically
sudo reboot
# After reboot:
systemctl --user status sensorpajen
Uninstalling the Service
If you need to remove the service:
# Stop and disable
systemctl --user stop sensorpajen
systemctl --user disable sensorpajen
# Remove service file
rm ~/.config/systemd/user/sensorpajen.service
# Reload systemd
systemctl --user daemon-reload
# Optionally disable lingering
sudo loginctl disable-linger $USER
Notes
- User Service: Runs as your user, not root - more secure and easier to manage
- Lingering: Required for services to run when not logged in
- Logs: All output goes to systemd journal (journalctl)
- Auto-restart: Service restarts automatically on crashes
- Environment: Config loaded from
config/sensorpajen.env - Working Directory: Service runs from
~/sensorpajen
Next Steps
Once the service is working:
- Monitor for a few days to ensure stability
- Check logs occasionally:
journalctl --user -u sensorpajen --since yesterday - Service will survive reboots and automatically restart on failures