man 5 systemd.exec

man 5 systemd.exec

我有许多运行 ruby​​ rake 作业所需的环境变量。此作业将由 systemd 监督。

我如何导出所有环境变量?

并以这样的方式导出它们只有我的 systemd 服务如果可能的话,会看到它们。

这是我的服务,其中只有一个环境变量:

  $ 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

我不想使用密钥,Environment因为我有很多环境变量

答案1

man 5 systemd.exec

并搜索指令“EnvironmentFile”,它应该会按照您的预期执行,请查看下面的摘录

   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.

相关内容