CodexBloom - Programming Q&A Platform

implementing Custom Systemd Service scenarios to Start on Fedora 36 with Missing Environment Variables

πŸ‘€ Views: 48 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-24
systemd fedora python

I'm working on a personal project and I'm experiencing issues with a custom systemd service not starting on Fedora 36... The service is designed to run a Python script that requires certain environment variables to be set, but it appears that these variables are not being recognized when the service attempts to start. Here's a snippet of my service file located at `/etc/systemd/system/my_service.service`: ```ini [Unit] Description=My Custom Service After=network.target [Service] Type=simple Environment=MY_VAR=value ExecStart=/usr/bin/python3 /path/to/my_script.py Restart=on-failure [Install] WantedBy=multi-user.target ``` Despite having the `Environment` line in my service file, when I check the logs using `journalctl -u my_service.service`, I see the following behavior: ``` my_script.py: KeyError: 'MY_VAR' ``` To debug the situation, I attempted to run the script manually from the terminal, and it worked without any issues, indicating the environment variable is set correctly in my shell. I've also tried adding `EnvironmentFile` pointing to a separate file containing the environment variables, but the result was the same. Here’s how I attempted that: ```ini EnvironmentFile=/etc/my_service.env ``` The `/etc/my_service.env` file contains: ```bash MY_VAR=value ``` After making these changes, I executed `systemctl daemon-reload` and attempted to restart the service with `systemctl restart my_service`. However, I still encounter the `KeyError` for `MY_VAR`. Is there something I might be missing regarding how environment variables are managed in systemd services? Any insights on how to ensure that my environment variables are correctly loaded would be greatly appreciated. This is part of a larger web app I'm building. I'd really appreciate any guidance on this.