3

I have virtual machine of Linux with Ubuntu 18. When I am running this command

sudo systemctl start myservice.service

Getting error

● myservice.service - dummyservice in .NET Loaded: loaded (/lib/systemd/system/myservice.service; disabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2020-05-26 23:53:20 IST; 10min ago Process: 3634 ExecStart=/usr/bin/dotnet /home/linux/bin/Downloads/myservice.dll (code=exited, status=1/FAILURE)

May 26 23:53:19 arvind systemd[1]: Starting myservice in .NET... May 26 23:53:20 arvind dotnet[3634]: The user's home directory could not be determined. Set the 'DOTNET_CLI_HOME' environment variable to spec May 26 23:53:20 arvind systemd[1]: myservice.service: Control process exited, code=exited status=1 May 26 23:53:20 arvind systemd[1]: myservice.service: Failed with result 'exit-code'. May 26 23:53:20 arvind systemd[1]: Failed to start myservice in .NET.

How can I set environment variable 'DOTNET_CLI_HOME' environment variable to spec ?

steeldriver
  • 136,215
  • 21
  • 243
  • 336

2 Answers2

5

When using SystemD, you can define environment variables inside your unit. (this is per the SystemD docs on service files and declarations and configurations.)

Example syntax:

[Service]
...
Environment=VARNAME=VARCONTENTS

So in this case, try adding Environment=DOTNET_CLI_HOME=/temp to your Service declaration in your service file. Then run your typical:

sudo systemctl daemon-reload
sudo systemctl start myservice.service

which should then utilize the newer setup/environment variable as defined in SystemD.

Thomas Ward
  • 74,764
0

There is couple ways to set an environment variables in Linux, and you can apply for Ubuntu.

1) Using export

export NAME=VALUE

2) Editing bashrc file in $USER folder

[... Other environments variables]
NAME=VALUE

If you want to know more and go deep the difference between then, please take a minute to read https://www.geeksforgeeks.org/environment-variables-in-linux-unix/

KpsLok
  • 129
  • 6
  • 3
    This may work for normal environment variables, etc. but this will NOT apply for SystemD units which are all run independently AND can be run as separate users in chroots, etc. For this, you have to go the SystemD route of defining environment variables for the service - see https://www.freedesktop.org/software/systemd/man/systemd.service.html – Thomas Ward May 27 '20 at 11:48