Import PATH variable into systemd unit

systemd

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?

Best Answer

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.