Skip to main content

Systemd service for a ruby rake job and environmental variables [Resolved]

I have many environmental variables that are required to run my ruby rake job. This job will be supervised by systemd.

How can I export all the environmental variables?

And also export them in such a way so that only my systemd service will see them, if this possible.

Here's my service which has only a single environmental variable in it:

  $ cat /etc/systemd/system/my_service.service 

  ExecStart=/bin/bash -lc 'source /home/ubuntu/.profile && bundle exec rake jobs:job1'


I don't want to use the key Environment because I have many env. variables

Question Credit: Maddani
Question Reference
Asked December 5, 2018
Posted Under: Network
1 Answers

man 5 systemd.exec

and search for directive "EnvironmentFile", it should do what you expect, please have a look at the extract below

       Similar to Environment= but reads the environment variables from a text file. The text file should contain new-line-separated variable
       assignments. Empty lines, lines without an "=" separator, or lines starting with ; or # will be ignored, which may be used for
       commenting. A line ending with a backslash will be concatenated with the following one, allowing multiline variable definitions. The
       parser strips leading and trailing whitespace from the values of assignments, unless you use double quotes (").

       The argument passed should be an absolute filename or wildcard expression, optionally prefixed with "-", which indicates that if the
       file does not exist, it will not be read and no error or warning message is logged. This option may be specified more than once in
       which case all specified files are read. If the empty string is assigned to this option, the list of file to read is reset, all prior
       assignments have no effect.

       The files listed with this directive will be read shortly before the process is executed (more specifically, after all processes from
       a previous unit state terminated. This means you can generate these files in one unit state, and read it with this option in the

       Settings from these files override settings made with Environment=. If the same variable is set twice from these files, the files will
       be read in the order they are specified and the later setting will override the earlier setting.

credit: Chaoxiang N
Answered December 5, 2018
Your Answer