While I was trying to write a service that uses a user's environment variables (loaded via .profile), I noticed that systemctl show-environment --user (run as e.g. myuser) would show me a certain set of variables that don't seem to be accessible to the service (also run as the myuser). Here's one way you could see this:
in your ~/.profile:
export MY_ENV_VAR="some value"
then login again so that you get this:
> echo $MY_ENV_VAR
"some value"
and:
> systemctl show-environment --user
... a bunch of env vars
MY_ENV_VAR="some value"
... a bunch more env vars
Then write a service e.g. do-the-thing.service:
[Unit]
Description=Use with timer to do the thing
[Service]
User=myuser
Group=myuser
ExecStart=/home/myuser/utility/do-the-thing.sh
and script do-the-thing.sh:
#!/usr/bin/env bash
echo "What's the value of MY_ENV_VAR?:"
echo $MY_ENV_VAR # For illustrative purposes
... do the thing
Now when you load this service up into systemd and run it manually with:
sudo systemctl start do-the-thing.service
You get nothing from the line that echoes MY_ENV_VAR, showing that it is not set.
I've since resolved the overarching issue by running the script using:
ExecStart=/bin/bash -lc /home/myuser/utility/do-the-thing.sh
But I'm still curious as to what environment systemctl show-environment --user is referring to, and why it's different from what I get when I run the service using:
User=myuser
Group=myuser
Thanks.