如何在启动时将 .NET Core 控制台应用程序作为 raspberry pi 的(守护进程)服务执行

How to execute a .NET Core console application on startup as a (daemon) service for the raspberry pi

我想知道如何 运行 将 dotnet 控制台应用程序作为 raspberry pi 上的服务。

我可以通过键入“”手动启动应用程序。/dotnet/garagedeur_pi/publish/Garagedeur“”

我创建了一个 Garagedeur.Service 文件:

Description=Garagedeur

[Service]
ExecStart=/home/pi/dotnet/garagedeur_pi/publish/Garagedeur.dll
WorkingDirectory=/home/pi/dotnet/garagedeur/pi/publish
#Restart=always
#RestartSec=10
User=pi

[Install]
WantedBy=multi-user.target

当我检查状态时,我得到这个:

● Garagedeur.service - Garagedeur
   Loaded: loaded (/lib/systemd/system/Garagedeur.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2020-02-17 12:09:49 CET; 11min ago
  Process: 1142 ExecStart=/home/pi/dotnet/garagedeur_pi/publish/Garagedeur.dll (code=exited, status=200/CHDIR)
 Main PID: 1142 (code=exited, status=200/CHDIR)

Feb 17 12:09:49 raspberrypi systemd[1]: Started Garagedeur.
Feb 17 12:09:49 raspberrypi systemd[1142]: Garagedeur.service: Changing to the requested working directory failed: No such file or directory
Feb 17 12:09:49 raspberrypi systemd[1142]: Garagedeur.service: Failed at step CHDIR spawning /home/pi/dotnet/garagedeur_pi/publish/Garagedeur.dll: No such file or directory
Feb 17 12:09:49 raspberrypi systemd[1]: Garagedeur.service: Main process exited, code=exited, status=200/CHDIR
Feb 17 12:09:49 raspberrypi systemd[1]: Garagedeur.service: Failed with result 'exit-code'.

非常感谢任何帮助。

解决方案:

Just read the error message and see that garagedeur/pi/ is different from garagedeur_pi/. Also there is a difference between Garagedeur.dll and Garagedeur – M. Spiller Feb 17 at 11:33

您可以在这里查看如何创建服务文件Host ASP.NET on Linux with Nginx。 在您的服务文件中,您必须添加 .NET 安装位置,如下所示:

ExecStart=/usr/bin/dotnet /home/pi/dotnet/garagedeur_pi/publish/Garagedeur.dll

举个例子:

[Unit]
Description=Garagedeur

[Service]
ExecStart=/usr/bin/dotnet /home/pi/dotnet/garagedeur_pi/publish/Garagedeur.dll
WorkingDirectory=/home/pi/dotnet/garagedeur/pi/publish
Restart=always
RestartSec=10
User=pi

[Install]
WantedBy=multi-user.target

路径 /usr/bin/dotnet 取决于您是否安装了 .NET。在我的 raspberry pi 上,这条路径是 /home/pi/dotnet-arm32/dotnet。您可以使用命令

找到正确的路径

dotnet --info