0

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.

2 Answers 2

1

systemctl show-environment --user talks to the user systemd instance. It is normally started when user logs in and remains active for as long as user remains logged in. This environment is set for any service started by the user systemd instance (like systemctl --user start foo.service).

Here is user's instance:

bor@ThinkPad-E16-Gen3:~/tmp$ LANG=C ps -fp 1681
UID          PID    PPID  C STIME TTY          TIME CMD
bor         1681       1  0 Jan17 ?        00:00:15 /usr/lib/systemd/systemd --user
bor@ThinkPad-E16-Gen3:~/tmp$ 

You are setting User or Group property for a system service. These services are spawned by the system systemd instance (normally PID 1) and inherit environment of the PID 1, not of the corresponding user's instance. Setting User does not make it user service - it simply runs all processes with this user's UID.

System and user systemd instances are independent processes, each having its own environment.

0

As you found out (using the -l switch: "Make bash act as if it had been invoked as a login shell"), .profile is only read by login shells.

A system-wide service that just happens to run as your user (i.e. /etc/systemd/system/… with User=) will not see those at all, but you might be interested in the systemd configuration clauses Environment and EnvironmentFile.

A user-local service (i.e. lives in ~/systemd/user/…, usually cannot have explicit User= configuration at all) should see all variables systemctl --user show-environment tells you, and potentially more.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.