大网数据广告活动
服务器运维问答

Import PATH variable into systemd unit

· 52CE 我爱测

I have a systemd service that looks like this: [Unit] Description=Kcrypt Backend Webpack Bundler After=network.target [Service] User=kenny Environment=NODE_ENV=PROD WorkingDirectory=/var/www/kcrypt-api/ ExecStart=/var/www/kcrypt-…

问题原文

原帖链接 · Hristo Kolev · CC BY-SA 3.0

I have a systemd service that looks like this:

[Unit]
Description=Kcrypt Backend Webpack Bundler
After=network.target

[Service]
User=kenny
Environment=NODE_ENV=PROD
WorkingDirectory=/var/www/kcrypt-api/
ExecStart=/var/www/kcrypt-api/scripts/webpack.sh

[Install]
WantedBy=multi-user.target

The .sh file looks like this:

#!/usr/bin/env bash

export NODE_ENV=DEV

rm ./dist/* -rf

yarn start webpack --watch 

The problem is that it cannot find yarn.

My user is called kenny. kenny has a tool called nvm installed that manages nodejs versions. That tool adds a directory to the PATH env variable by editing '~/.bashrc'.

That means that the yarn command is only available if the user is logged in as kenny.

I was left with the impression that if I set the systemd's unit's user to 'kenny', systemd will take care of the rest, or I don't know what I was thinking.

Is there any way that I can import kenny's PATH variable into the systemd unit?

已采纳解答

原帖链接 · Andrew · CC BY-SA 3.0

Instead of trying to 'reference a user's PATH variable', you should either define PATH manually, or more appropriately, use the full path to the binary.

That means that the yarn command is only available if the user is logged in as kenny

This isn't accurate. It just means that yarn isn't installed in a location referenced by the PATH variable used by systemd, and is likely in a custom location, or location outside of the default PATH variable.

When you're logged in as kenny, use which yarn to print the path to yarn, then make sure to use that when referencing it in the script.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-04-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。