15

I am using systemd service for my script I need to set environment vaules from a home/user/.bashrc

source /home/user/.bashrc not works in script and systemd seed don't support sourcing function. Help me

8
  • Is there a reason you want to use a .bashrc rather than an EnvironmentFile? Apr 11, 2018 at 1:21
  • 1
    I use commands that works on user terminal
    – user8893236
    Apr 11, 2018 at 1:36
  • when i use command they said me cannot find path
    – user8893236
    Apr 11, 2018 at 1:36
  • and some variables that i exported in bashrc need to my command
    – user8893236
    Apr 11, 2018 at 1:36
  • 1
    Please check How to ask question
    – Morse
    Apr 11, 2018 at 1:38

1 Answer 1

29

If you must...

Instead of trying to generate an EnvironmentFile, have a shell execute your startup scripts and then execute your command. This avoids steps that can introduce a mismatch (as between how env stores your environment, and how the systemd EnvironmentFile option loads it).


To source your target user's startup scripts:

[Service]
Type=simple
User=user
Group=user
ExecStart=/bin/bash -l -c 'exec "$@"' _ your-command arg1 arg2 ...

To source an arbitrary file:

Here, instead of using bash -l to run a login shell, we explicitly source $0, and pass /home/user/.bashrc in that position.

[Service]
Type=simple
User=user
Group=user
ExecStart=/bin/bash -c '. "$0" && exec "$@"' /home/user/.bashrc your-command arg1 arg2 ...

But Don't. Really.

  • .bashrc files are generally intended for setting up interactive environments. This means that their settings are often not appropriate for services.
  • Building a separate EnvironmentFile that you hand-audit for your service means you know exactly what the service is running with, and can configure it separately from the interactive environment. If you've hand-audited that EnvironmentFile to have the same meaning when executed by a shell, you could also run set -a; source /path/to/your-environment-file; set +a in your .bashrc to pull its environment variables in.
  • From a security perspective, it's generally unwise to let a service modify any executable code it runs -- providing such permissions means that an attacker who has breached a service can make their breach persistent even without any secondary privilege escalation attacks. Using an EnvironmentFile in a non-user-writable location like /etc/conf.d is thus safer than a dotfile under that user's home directory.
6
  • oh it seems much simple, Let me check if it works ;) Thank you @Charles
    – user8893236
    Apr 11, 2018 at 1:54
  • okay Charles, You seems much good than I, I will try yours advise
    – X zheng
    Apr 11, 2018 at 2:01
  • If the script is meant to run dependent of the user's shell environment then calling it with bash -l is right and using a static EnvironmentFile file is wrong. There are variables that can be dynamically initialized by Bash's initialization scripts. Obviously you might suggest moving these dynamic initializations to systemd but that isn't always adaptable and practical.
    – konsolebox
    Jan 7, 2022 at 16:18
  • I needed to put the entire bash command into quotation marks: ExecStart=/bin/bash -c '. "$0" && exec "$@" /home/user/.bashrc your-command arg1 arg2 ...'
    – Philipp
    Jun 21, 2022 at 16:08
  • @Philipp Doing that defeats the purpose of using "$@". In particular, when you do that, you don't have a $0 or a $@ because everything goes into the -c argument to be run as a script. Indeed, I'm surprised it works at all -- I would have expected the . "$0" to fail (on account of $0 not being set to the name of a sourceable script), and consequently everything else to not be run at all. Jun 21, 2022 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.