[ASP.NET 5] Production Ready Web Server on Linux. Run Kestrel In The Background

By | June 12, 2015

vNextIn the previous article we have installed nginx as a “gate” for our ASP.NET 5 application. Today we will see how to start kestrel server in the background. We will do even more, we will create init.d script to control our APS.NET 5 application and start it on  the system’s startup.

The simplest way to start kestrel in the background from console:

nohup k kestrel &

But if you want to start your application at the system’s startup you need init.d script.

Eventually, we will have following commands in out system:

To start APS.NET 5 application:

sudo service kestrel start

To stop:

sudo service kestrel stop

And to restart:

sudo service kestrel restart

First, you should download init.d script template from github:

wget "https://gist.githubusercontent.com/drussilla/3182463b9fa1ec94b9db/raw/da5f31db2226a0a29a4391c1c69d61a444d1704a/kestrel_service.sh"

Here is script’s content:

# Provides:          <SCRIPT_NAME>
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Script to run asp.net 5 application in the background

# Author: Ivan Derevianko aka druss <drussilla7@gmail.com>

DNXRUNTIME=<PATH_TO_RUNTIME> #/home/$WWW_USER/.k/runtimes/kre-mono.1.0.0-beta3/bin/k


# fix issue with DNX exception in case of two env vars with the same name but different case
unset runlevel

start() {
  if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
    echo 'Service already running' >&2
    return 1
  echo 'Starting service...' >&2
  su -c "start-stop-daemon -SbmCv -x /usr/bin/nohup -p \"$PIDFILE\" -d \"$APPROOT\" -- \"$DNXRUNTIME\" kestrel > \"$LOGFILE\"" $WWW_USER
  echo 'Service started' >&2

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Service not running' >&2
    return 1
  echo 'Stopping service...' >&2
  start-stop-daemon -K -p "$PIDFILE"
  rm -f "$PIDFILE"
  echo 'Service stopped' >&2

case "$1" in
    echo "Usage: $0 {start|stop|restart}"

export runlevel=$TMP_SAVE_runlevel_VAR

We need to unset runlevel variable and then restore it at the end, because during init process we will have two variables, runlevel and RUNLEVEL, but ASP.NET 5 cannot work with two variables with the same name in different case. You will get “System.ArgumentException: An element with the same key already exists in the dictionary.” exception

As you can see, you need to change some variables inside the script:

<SCRIPT_NAME> – name of the script. Should be the same as file name. In out example it is kestrel

<USERNAME> – run the application under this user. The user should have DNVM installed and have access to the APPROOT path

<PATH_TO_RUNTIME> – path to dnx script

<PATH_TO_APPLICATION> – full path to your application

Copy this script to /etc/init.d folder. And rename it to the <SCRIPT_NAME>

sudo cp kestrel_service.sh /etc/init.d/<SCRIPT_NAME>

Make it executable

sudo chmod +x /etc/init.d/<SCRIPT_NAME>

It is time to test our script. Try to start your application with command:

sudo service <SCRIPT_NAME> start

Now go to a browser and try to access your application. If it is not working you can check log in your application’s directory (kestrel.log).

Start ASP.NET 5 application automatically:

sudo update-rc.d <SCRIPT_NAME> defaults

To remove it from startup:

sudo update-rc.d -f <SCRIPT_NAME> remove

If you have multiple applications just create a script with different <SCRIPT_NAME> for every application you have.

3 thoughts on “[ASP.NET 5] Production Ready Web Server on Linux. Run Kestrel In The Background

  1. Daniel Lo Nigro

    Rather than using an init script, I’d suggest using Supervisord for running Kestrel. You can set it up so it restarts Kestrel if it crashes.


Leave a Reply

Your email address will not be published. Required fields are marked *