Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Tragetaschen/435c5bb2062c29753070 to your computer and use it in GitHub Desktop.
Save Tragetaschen/435c5bb2062c29753070 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Microsoft.Framework.Runtime;
using Mono.Unix;
public static class ApplicationShutdownExtensionMethods
{
public static void OnUnixSignals(this IApplicationShutdown applicationShutdown)
{
Task.Factory.StartNew(() =>
{
var sigInt = new UnixSignal(Mono.Unix.Native.Signum.SIGINT);
var sigTerm = new UnixSignal(Mono.Unix.Native.Signum.SIGTERM);
Console.WriteLine("To terminate, send SIGINT or SIGTERM to the process");
UnixSignal.WaitAny(new[] { sigInt, sigTerm });
Console.WriteLine("Received signal");
applicationShutdown.RequestShutdown();
}, TaskCreationOptions.LongRunning);
}
}
[Unit]
Description=ASP.NET 5 application
[Service]
WorkingDirectory=/opt/something
User=not-root
ExecStart=/bin/bash kestrel
Environment=TEMP=/tmp
Type=notify
NotifyAccess=main
[Install]
WantedBy=multi-user.target
public void Configure(
IApplicationLifetime applicationLifetime,
IApplicationShutdown applicationShutdown
)
{
applicationLifetime.NotifySystemd();
applicationShutdown.OnUnixSignals();
}
using System.Runtime.InteropServices;
using Microsoft.AspNet.Hosting;
public static class SystemdStartupNotifier
{
public static void NotifySystemd(this IApplicationLifetime applicationLifetime)
{
applicationLifetime.ApplicationStarted.Register(handler);
}
private static void handler()
{
sd_notify(0, "READY=1");
}
[DllImport("libsystemd.so.0")]
private static extern int sd_notify(int unset_environment, string state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment