I am trying to use a ready-made bash script that sets env. This is the service that I'm trying to use:

Description=myserver service

ExecStart=/bin/sh -c '/home/ec2-user/myserver/config/myserverVars.sh ;/home/ec2-user/venv/bin/python  /home/ec2-user/myserver/myserver.py 2>&1 >> /home/ec2-user/myserver/logs/systemd_myserver.log' 


The myserverVars.sh:

export [email protected]
export APP2_BIND_PASS=xxxxxx
export APP3=xxxxxx

the variables in /home/ec2-user/myserver/config/myserverVars.sh
are never set, and the server is started without the variables and this is wrong. I am trying to avoid using Environment key or Environment File.

  • Note that Environment is the native mechanism in systemd, and the service unit file is intended to directly contain environment settings. The systemd people consider EnvironmentFile to have been a mistake, but conversely Environment is the way to set environment variables, either directly or with drop-in "snippet" files. unix.stackexchange.com/a/557081/5132
    – JdeBP
    Commented Jun 12, 2020 at 13:10

2 Answers 2


If you want variables defined in a script file to be available to the parent environment running that script, you need to source the script, not execute it. Change your ExecStart line to:

ExecStart=/bin/sh -c '. /home/ec2-user/myserver/config/myserverVars.sh ;/home/ec2-user/venv/bin/python  /home/ec2-user/myserver/myserver.py >> /home/ec2-user/myserver/logs/systemd_myserver.log 2>&1 ' 

See What is the difference between sourcing ('.' or 'source') and executing a file in bash? for details on the difference between sourcing and executing a script.

Also note that I changed the order of redirections. To get both stderr and stdout to the same file, you need > file 2>&1 not 2>&1 > file.

  • any way to format this long string to something readble ? with line breaks "\"
    – user63898
    Commented Jun 11, 2020 at 13:39
  • @user63898 I don't know, did you try it with line breaks? If they dont' work, please ask a new question since that is a completely separate topic.
    – terdon
    Commented Jun 11, 2020 at 14:06
  • Of course, the redirections are a bad idea in the first place, as service management systems handle logging standard output/error. And there should be an exec in the invocation of python, to cope with the case that /bin/sh is the Bourne Again shell.
    – JdeBP
    Commented Jun 12, 2020 at 13:06

For me this worked:

 ExecStart=/bin/bash -c 'MYVAR=xx exec /some/file'
  • 2
    ...except that they are "trying to use a ready-made bash script that sets env", so hard-coding them independently in the systemd unit file risks configuration drift if someone edits the script but forgets to change the unit file.
    – Jeff Schaller
    Commented Mar 30, 2022 at 17:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .