You are not logged in.

#1 2025-09-13 23:51:27

gattu_marrudu
Member
Registered: 2008-03-29
Posts: 80

[SOLVED] User unit environment variables ignored

Hello,
I am writing a user service for a custom app started by an "apprun" user. The user has linger mode login on, so the service should start at boot.

I wrote a unit file:

$ cat ~/.config/systemd/user/pkar_watch.service 
[Unit]
Description=Pocket archive watchdog

[Service]
ExecStart=pkar_watch -g -c -f ${PKAR_ROOT}/log/watch.log -l 3 ${PKAR_ROOT}/watch

[Install]
WantedBy=default.target

And an environment file:

$ cat ~/.config/environment.d/pkar_watch.conf 
PKAR_CONFIG_DIR="${HOME}/.config/pkar"
PKAR_ROOT="/data/pocket_archive"

I want to reuse those environments for the interactive shell, so I added to my .bashrc:

systemctl --user import-environment PKAR_CONFIG_DIR
systemctl --user import-environment PKAR_ROOT

But when I log into Bash as the apprun user, I get the message:

Environment variable $PKAR_CONFIG_DIR not set, ignoring.
Environment variable $PKAR_ROOT not set, ignoring.

Indeed the variables are not loaded.

Incidentally, the service also fails to start but I cannot see any journals with journalctl --user -u pkar_watch.

What's wrong with my setup? Is it related to the service failure, or to why it's not writing any readable journal? (update: I actually got the service to start)

Thanks,
gm

Last edited by gattu_marrudu (2025-09-14 17:50:12)

Offline

#2 2025-09-14 00:16:00

gattu_marrudu
Member
Registered: 2008-03-29
Posts: 80

Re: [SOLVED] User unit environment variables ignored

Update: I got to see the logs in the journal, but only as root. I am not sure why I cannot see the user unit journal as the unit owner.

Offline

#3 2025-09-14 08:06:26

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 69,173

Re: [SOLVED] User unit environment variables ignored

"systemctl --user import-environment" imports the environment into the user session, https://wiki.archlinux.org/title/System … _variables
https://wiki.archlinux.org/title/Enviro … nd_session randomly has a pattern on how to get the environment from system into the shell

And inb4 you ask: it's because systemd is the ultimate NIH vanity project.

Online

#4 2025-09-14 16:38:35

gattu_marrudu
Member
Registered: 2008-03-29
Posts: 80

Re: [SOLVED] User unit environment variables ignored

Thanks, Seth. My systemctl --user show-environment shows the variables I set in the user's environment.d, but systemctl --user set-environment says it can't find them.

I am thinking about moving the unit to a system-level one started by root and run as the apprun user, but I am not sure if that would improve the environment setup situation.

Offline

#5 2025-09-14 17:07:13

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 414

Re: [SOLVED] User unit environment variables ignored

gattu_marrudu wrote:

but systemctl --user set-environment says it can't find them.

"systemctl --user set-environment ..." sets specified environment for a session. It looks like you want the reverse: set shell environment according to the session.
Shell run systemctl as a child process. Child process never can modify parent's environment. You can do something like this in your .bashrc (or .bash_profile):

eval $(systemctl --user show-environment)

However, this also doesn't look like a nice solution. Consider something like creating /etc/pkar.conf with variables definition, then import them into service with EnvironmentFile= as well as import to a shell with "source" command from shell startup file.

Offline

#6 2025-09-14 17:49:57

gattu_marrudu
Member
Registered: 2008-03-29
Posts: 80

Re: [SOLVED] User unit environment variables ignored

dimich wrote:

"systemctl --user set-environment ..." sets specified environment for a session. It looks like you want the reverse: set shell environment according to the session.

Oops, I meant systemctl --user import-environment.

dimich wrote:

Consider something like creating /etc/pkar.conf with variables definition, then import them into service with EnvironmentFile= as well as import to a shell with "source" command from shell startup file.

I like this better too, thanks for the tip. Except that I would use a non-privileged location if I keep the service as a user unit.

Offline

#7 2025-09-14 18:40:55

dimich
Member
From: Kharkiv, Ukraine
Registered: 2009-11-03
Posts: 414

Re: [SOLVED] User unit environment variables ignored

gattu_marrudu wrote:

Oops, I meant systemctl --user import-environment.

systemctl --user import-environment VARIABLE

has the same effect as

systemctl --user set-environment VARIABLE="$VARIABLE"

with the only difference that latter works even if VARIABLE is not exported to shell's environment.

Offline

Board footer

Powered by FluxBB