我有一台装有 Ubuntu 18 的 Linux 虚拟机。当我运行此命令时
sudo systemctl start myservice.service
获取错误
● 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.
我如何设置环境变量'DOTNET_CLI_HOME' environment variable to spec
?
答案1
使用 SystemD 时,您可以在单元内部定义环境变量。(这是根据 SystemD 文档关于服务文件和声明和配置。
示例语法:
[Service]
...
Environment=VARNAME=VARCONTENTS
因此,在这种情况下,请尝试Environment=DOTNET_CLI_HOME=/temp
在服务文件中添加到服务声明中。然后运行您的典型操作:
sudo systemctl daemon-reload
sudo systemctl start myservice.service
然后应该利用 SystemD 中定义的较新的设置/环境变量。
答案2
在 Linux 中有几种设置环境变量的方法,您可以应用于 Ubuntu。
1)使用导出
export NAME=VALUE
2)编辑$USER文件夹中的bashrc文件
[... Other environments variables]
NAME=VALUE
如果你想了解更多并深入了解它们之间的区别,请花一分钟阅读https://www.geeksforgeeks.org/environment-variables-in-linux-unix/