You are not logged in.
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
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
"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.
Offline
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
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
"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.
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
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