我正在尝试创建一个与systemd
Init System一起使用的简单回声服务。一切都还好,甚至在快速部署之后开始,但是当我试图通过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(SystemD配置文件(
[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
help-scrips,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
日志:
- 构建/部署:https://pastebin.com/9hwbxrcx
- 来自
journalctl -fu hw.service
:https://pastebin.com/ssmnbgts - 从
systemctl status hw.service
,首次部署之后:https://pastebin.com/3hysgdsi
我在等什么?
我希望我的服务运行,因为我的其他服务(ASP.NET核心(正在运行(Active/Green状态(作为systemd
服务。至于ASP.NET核心项目,没有问题,因为简单的控制台 - 它们是...
如何解决我的问题?
谢谢
AS 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();
}
}
}
现在一切都在工作&amp;服务不会停止/崩溃。