0

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 
  [Unit]
  After=syslog.target
  Requires=mysql.service

  [Service]
  WorkingDirectory=/home/ubuntu/my_app/app1
  ExecStart=/bin/bash -lc 'source /home/ubuntu/.profile && bundle exec rake jobs:job1'
  Restart=on-abort
  RestartSec=10
  StandardOutput=syslog
  StandardError=syslog
  SyslogIdentifier=app1_bg_jobs
  User=ubuntu
  Group=ubuntu
  Environment=RAILS_ENV=staging

  [Install]
  WantedBy=multi-user.target

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

1
  • systemd will substitute $HOME correctly in an environment variable, if the unit defines User= as yours does. Dec 4, 2018 at 16:38

1 Answer 1

0

man 5 systemd.exec

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

   EnvironmentFile=
       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
       next).

       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.
4
  • $ man 5 systemd.exec ---> No entry for systemd.exec in section 5 of the manual
    – Maddani
    Dec 4, 2018 at 18:30
  • [email protected]:~# find /usr/share/man -name *systemd.exec* /usr/share/man/man5/systemd.exec.5.gz Dec 4, 2018 at 18:33
  • it finds nothing. I'm on Mac.
    – Maddani
    Dec 4, 2018 at 18:40
  • @Maddani Macs don't use systemd. Look up man pages on the correct OS. Dec 4, 2018 at 21:37

You must log in to answer this question.

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