命令行 - 在.NET systemd service中,如何设置环境变量?

分享于 

2分钟阅读

ubuntu

  繁體

问题:

我使用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时,可以在单元内定义环境变量,

语法示例:


[Service]


...


Environment=VARNAME=VARCONTENTS



在这种情况下,请尝试将Environment=DOTNET_CLI_HOME=/temp添加到服务文件中的服务声明中,


sudo systemctl daemon-reload


sudo systemctl start myservice.service



然后应该使用SystemD中定义的较新的setup/environment变量。


答案2:

有两种方法可以在环境变量中设置Linux,并且Ubuntu可以使用。

1)使用export

 
export NAME=VALUE



2)在$USER文件夹中编辑bashrc文件


[... Other environments variables]


NAME=VALUE





COM  COMM  SYS  系统  SET  Comma