如何从 systemd 配置文件中的函数输出设置 ExecStart?

How to set ExecStart from function output in systemd config file?

我在目录 /lib/systemd/system/ 中创建了一个 systemd 服务文件来启动 nodejs 作为服务。问题是该文件在 ExecStart 字段中包含指向 nodej 的硬编码路径。这意味着我每次更新 nodejs 时都必须手动更改此服务文件。有没有办法在下面的文件中使用which node命令来自动设置路径?

[Unit]
Description=node-server-1
After=network.target

[Service]
Environment=NODE_PORT=3001
Type=simple
User=manid
ExecStart=/home/manidos/.nvm/versions/node/v14.15.1/bin/node /home/manidos/node-nginx/index.js
Restart=on-failure

[Install]
WantedBy=multi-user.target

您可以在 ExecStart 行中 运行 一个简单的 bash 脚本:

ExecStart=/bin/bash -c '$$(/usr/bin/which node) /home/manidos/node-nginx/index.js'

基本上,这是 运行 在子 shell 中执行 which 命令,并通过外部脚本将其标准输出传递给 运行。如 systemd.service manpage.

中所述,在 systemd 单元文件中使用此特定语法需要双倍费用