Home > OS >  Linux / Raspberry Pi OS - Setting environment variable that will work with a systemd service
Linux / Raspberry Pi OS - Setting environment variable that will work with a systemd service

Time:08-14

I asked this question yesterday which in essence is regarding running a python3 script (that uses an environment variable set in etc/enviroment) that is run by a systemd service which I've named mqtt.service.

I got some useful information from an answer to that question indicating that by default environment variables in /etc/environment aren't accessible in systemd, and that answer linked to this question that details the steps you can take to allow the systemd service to see an enviroment variable of your choosing.

So far I've tried the following:

Within my service I’ve tried to add EnvironmentFile=/etc/environment which contains my environment variable: export DEVICE_ID="TEST1". I’ve also tried using the systemctl edit mqtt.service to create mqtt.service.d/override.conf file that also has EnvironmentFile=/etc/environment set.

Next I tried instead to set the Environment=$DEVICE_ID in the mqtt.service directly as well as the mqtt.service.d/override.conf file.

I then tried all of the above but changing where the $DEVICE_ID environment variable was set. So far I’ve tried setting it in etc/profile with a value of export DEVICE_ID="TEST1".

Finally I also tried creating a device_id.sh file within /etc/profile.d/device_id.sh which has a value of export DEVICE_ID=TEST1. Which gave me the following results when i tried to run the mqtt.service:

user@computer:/etc/profile.d $ sudo systemctl status mqtt.service
● mqtt.service - Arms the mqtt.py script that will alert if the device is moved
     Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/mqtt.service.d
             └─override.conf
     Active: activating (auto-restart) (Result: exit-code) since Fri 2022-08-12 14:49:52 BST; 4s ago
    Process: 12941 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 12941 (code=exited, status=1/FAILURE)
        CPU: 551ms

user@computer:/etc/profile.d $ sudo systemctl status mqtt.service
● mqtt.service - Arms the mqtt.py script that will alert if the device is moved
     Loaded: loaded (/etc/systemd/system/mqtt.service; disabled; vendor preset: enabled)
    Drop-In: /etc/systemd/system/mqtt.service.d
             └─override.conf
     Active: activating (auto-restart) (Result: exit-code) since Fri 2022-08-12 14:49:52 BST; 4s ago
    Process: 12941 ExecStart=python3 /scripts/mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 12941 (code=exited, status=1/FAILURE)
        CPU: 551ms
user@computer:/etc/profile.d $

Everything I tried above gave me simular error messages when trying to run the mqtt.service.

I'm really struggling with this, I don't know If I'm, missing something or is it the case that what I'm trying to achieve isn't possible?

CodePudding user response:

As explained in the second answer to the link you gave, and man environment.d, the file /etc/environment is used to set variables in the user services environment, i.e. services started with systemctl --user start .... You are using system services, so if you want to include this file you needed to set

[Service]
EnvironmentFile=/etc/environment

which you probably have by now. Make sure it is in the right section. You also need to run sudo systemctl daemon-reload after editing a unit file.

However, the man page also explains that the format for lines in the environment file is extremely limited. In particular, the shell command export is not allowed. Although it is not mentioned, double-quotes are allowed, but not needed. So you should try an entry like

DEVICE_ID=TEST1

You should do a systemctl --user daemon-reload after editing this file for it to be parsed and checked for errors, but you need to look for the error messages in one of the various system log files. You can see any changes with systemctl --user show-environment.

  • Related