user4959035
user4959035

Reputation:

.NET Core 1.1 console app always exits/crashes as systemd service on Ubuntu 16.04

I'm trying to create a simple echo service, which works with the systemd init system. All is OK and it's even starting after quick-deploy, BUT it exits (or crashes) quickly, when I'm trying to figure out its state via systemctl status <service> command.

The logic is simple, here I'm providing the next sources:

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 config file)

[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-script, init.sh (you may also use chmod +x init.sh, if want to try it on your local system):

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

Logs:

What am I waiting for?

I want my service run as other my services (ASP.NET Core) are running (with active/green state) as systemd services. As for the ASP.NET Core projects, there are NO problems, as for the simple console - they are...

How to fix my problems?

Thanks

Upvotes: 2

Views: 1752

Answers (1)

user4959035
user4959035

Reputation:

As Evk has suggested to use ManualResetEvent, I've done the next:

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();
        }
    }
}

Now all is working & service doesn't stop/crash.

Upvotes: 1

Related Questions