Bash – Systemd bash builtins

bashenvironment-variablessourcesystemd

I have an application that needs to use the commando source for setting his environment file.

[Unit]
Description=Programname
After=syslog.target network.target

[Service]
User=root
ExecStartPre=source /opt/environmentname/bin/activate enviromentname
ExecStart=/var/programname/programname -f /etc/programname/programconfig.conf

[Install]
WantedBy=multi-user.target  

Now, Systemd complains about source because this isn't an absolute path and source is a builtin of bash so I tried

ExecStartPre=/bin/bash -c "source /opt/environmentname/bin/activate enviromentname"

That we run the script with source instead of chmod +x is because of vendor requirements.

So is there a way to get this working? Or do we need to work with forking or something?

How can we use bash builtins correctly inside Systemd unit files?

Best Answer

You should be able to do a multi-line script eg

ExecStart=/bin/bash -c '\
   source /opt/environmentname/bin/activate environmentname; \
   exec /var/programname/programname -f /etc/programname/programconfig.conf'