“systemd”文件中的变量?

Jod*_*ddy 6 linux systemd

我有这个 systemd 文件:

  [Unit]
  Description=something website
  After=syslog.target
  Requires=postgresql.service

  [Service]
  ExecStart=/home/my_user/my_webapp/bin/ foreground
  ExecStop=/home/my_user/my_webapp/bin/ stop
  Restart=on-abort
  WorkingDirectory=/home/my_user/my_webapp/bin
  SyslogIdentifier=my_webapp_web_app
  User=my_user

  [Install]
  WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

如您所见,“my_webapp”和“my_user”重复了很多次。有没有办法将它们存储到变量中并使用变量代替?

Zor*_*che 3

可能最符合您需求的就是使用该EnvironmentFile选项。

# file: /home/my_user/my_webapp/environment
# included in systemd unit my_webapp
# EnvironmentFile=-/home/my_user/my_webapp/environment
BASEDIR=/home/my_user/my_webapp
Run Code Online (Sandbox Code Playgroud)

还有你的新单位

[Unit]
Description=something website
After=syslog.target
Requires=postgresql.service

[Service]
ExecStart=$BASEDIR/bin/ foreground
ExecStop=$BASEDIR/bin/ stop
Restart=on-abort
WorkingDirectory=$BASEDIR/bin
SyslogIdentifier=my_webapp_web_app
User=my_user

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

您可能希望变量设置不同。我将把它留给你来调整。