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
  • 131,985
  • 21
  • 239
  • 326
  • Might help -- https://blog.jongallant.com/2018/09/solution-users-home-directory-could-not-be-determined/ – Liso May 27 '20 at 11:20
  • @Liso - In that workaround, they didnt mention where to do that. In Windows OS or in Virtual Linux machine or in .Net project? – Arvind Chourasiya May 27 '20 at 11:22
  • You need to set the env of the user running the 'myservice.service' process. – Andreas F May 27 '20 at 11:36
  • @AndreasF - How can I do that? – Arvind Chourasiya May 27 '20 at 11:38
  • https://help.ubuntu.com/community/EnvironmentVariables – Andreas F May 27 '20 at 11:42
  • @AndreasF - If you can edit your answer and add like how to env to user and to /temp path. That will be very nice. I am very new to Ubuntu. – Arvind Chourasiya May 27 '20 at 11:44
  • You may find the information here useful: [How to run a .NET Core console app as a service using Systemd on Linux](https://swimburger.net/blog/dotnet/how-to-run-a-dotnet-core-console-app-as-a-service-using-systemd-on-linux) and also [How do I make my systemd service run via specific user and start on boot?](https://askubuntu.com/questions/676007/how-do-i-make-my-systemd-service-run-via-specific-user-and-start-on-boot). However I can't help feeling that this is something you should be running as a [systemd user service](https://wiki.archlinux.org/index.php/Systemd/User) – steeldriver May 27 '20 at 11:49
  • @AndreasF won't work for SystemD units, the envs need to be defined by the app running OR by `Environment=` declaration with all the env vars available. – Thomas Ward May 27 '20 at 11:49
  • @steeldriver - Thank you. That issue has been solved. – Arvind Chourasiya May 27 '20 at 14:03

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
  • 72,494
  • 30
  • 173
  • 237
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