.NET Core 1.1 控制台应用程序始终 exits/crashes 作为 Ubuntu 16.04 上的系统服务
.NET Core 1.1 console app always exits/crashes as systemd service on Ubuntu 16.04
我正在尝试创建一个简单的回显服务,它与 systemd
初始化系统一起工作。一切正常,它甚至在快速部署后启动,但当我试图通过 systemctl status <service>
命令弄清楚它的状态时,它很快退出(或崩溃)。
逻辑很简单,我在这里提供下一个来源:
Program.cs
using System;
using System.Threading.Tasks;
namespace hw
{
class Program
{
private const int delay = 1000;
private static Random random = new Random();
static async Task Handle()
{
Console.WriteLine($"tick tack... {random.Next()}");
await Task.Delay(delay);
await Handle();
}
static void Main(string[] args)
{
Task.Factory.StartNew(async() => await Handle());
Console.ReadLine();
}
}
}
hw.service(系统配置文件)
[Unit]
Description=HW Echo Service
[Service]
WorkingDirectory=/var/netcore/hw
ExecStart=/usr/bin/dotnet /var/netcore/hw/hw.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-echo-hw
[Install]
WantedBy=multi-user.target
帮助脚本,init.sh(如果想在本地系统上尝试,您也可以使用 chmod +x init.sh
):
dotnet build
dotnet publish
echo "\nPreparing binaries for the service directory:\n"
rm -rf /var/netcore/hw
mkdir /var/netcore /var/netcore/hw
cp -R bin/Debug/netcoreapp1.1/publish/* /var/netcore/hw
ls -la /var/netcore/hw
echo "\nInitializing the systemd service:"
systemctl stop hw.service
rm /etc/systemd/system/hw.service
cp hw.service /etc/systemd/system/hw.service
systemctl daemon-reload
systemctl enable hw.service
systemctl start hw.service
systemctl status hw.service
日志:
- Build/Deploy: https://pastebin.com/9hwbxrCX
- 来自
journalctl -fu hw.service
:https://pastebin.com/SSMNbgtS
- 来自
systemctl status hw.service
,在第一次部署之后:https://pastebin.com/3HySgdsi
我还在等什么?
我想要我的服务 运行,因为我的其他服务(ASP.NET 核心)是 运行ning(具有 active/green 状态)作为 systemd
服务。至于 ASP.NET 核心项目,没有问题,至于简单的控制台 - 它们是...
如何解决我的问题?
谢谢
由于 Evk 建议使用 ManualResetEvent
,我做了下一个:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace hw
{
class Program
{
private const int delay = 1000;
private static Random random = new Random();
private static ManualResetEvent resetEvent = new ManualResetEvent(false);
static async Task Handle()
{
Console.WriteLine($"tick tack... {random.Next()}");
await Task.Delay(delay);
await Handle();
}
static void Main(string[] args)
{
Task.Factory.StartNew(async() => await Handle());
Console.CancelKeyPress += (sender, eventArgs) =>
{
// Cancel the cancellation to allow the program to shutdown cleanly.
eventArgs.Cancel = true;
resetEvent.Set();
};
resetEvent.WaitOne();
}
}
}
现在一切正常但服务不正常stop/crash。
我正在尝试创建一个简单的回显服务,它与 systemd
初始化系统一起工作。一切正常,它甚至在快速部署后启动,但当我试图通过 systemctl status <service>
命令弄清楚它的状态时,它很快退出(或崩溃)。
逻辑很简单,我在这里提供下一个来源:
Program.cs
using System;
using System.Threading.Tasks;
namespace hw
{
class Program
{
private const int delay = 1000;
private static Random random = new Random();
static async Task Handle()
{
Console.WriteLine($"tick tack... {random.Next()}");
await Task.Delay(delay);
await Handle();
}
static void Main(string[] args)
{
Task.Factory.StartNew(async() => await Handle());
Console.ReadLine();
}
}
}
hw.service(系统配置文件)
[Unit]
Description=HW Echo Service
[Service]
WorkingDirectory=/var/netcore/hw
ExecStart=/usr/bin/dotnet /var/netcore/hw/hw.dll
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-echo-hw
[Install]
WantedBy=multi-user.target
帮助脚本,init.sh(如果想在本地系统上尝试,您也可以使用 chmod +x init.sh
):
dotnet build
dotnet publish
echo "\nPreparing binaries for the service directory:\n"
rm -rf /var/netcore/hw
mkdir /var/netcore /var/netcore/hw
cp -R bin/Debug/netcoreapp1.1/publish/* /var/netcore/hw
ls -la /var/netcore/hw
echo "\nInitializing the systemd service:"
systemctl stop hw.service
rm /etc/systemd/system/hw.service
cp hw.service /etc/systemd/system/hw.service
systemctl daemon-reload
systemctl enable hw.service
systemctl start hw.service
systemctl status hw.service
日志:
- Build/Deploy: https://pastebin.com/9hwbxrCX
- 来自
journalctl -fu hw.service
:https://pastebin.com/SSMNbgtS - 来自
systemctl status hw.service
,在第一次部署之后:https://pastebin.com/3HySgdsi
我还在等什么?
我想要我的服务 运行,因为我的其他服务(ASP.NET 核心)是 运行ning(具有 active/green 状态)作为 systemd
服务。至于 ASP.NET 核心项目,没有问题,至于简单的控制台 - 它们是...
如何解决我的问题?
谢谢
由于 Evk 建议使用 ManualResetEvent
,我做了下一个:
using System;
using System.Threading;
using System.Threading.Tasks;
namespace hw
{
class Program
{
private const int delay = 1000;
private static Random random = new Random();
private static ManualResetEvent resetEvent = new ManualResetEvent(false);
static async Task Handle()
{
Console.WriteLine($"tick tack... {random.Next()}");
await Task.Delay(delay);
await Handle();
}
static void Main(string[] args)
{
Task.Factory.StartNew(async() => await Handle());
Console.CancelKeyPress += (sender, eventArgs) =>
{
// Cancel the cancellation to allow the program to shutdown cleanly.
eventArgs.Cancel = true;
resetEvent.Set();
};
resetEvent.WaitOne();
}
}
}
现在一切正常但服务不正常stop/crash。