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:

#!/bin/sh
### BEGIN INIT INFO
# 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
### END INIT INFO

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

WWW_USER=<USERNAME>
DNXRUNTIME=<PATH_TO_RUNTIME> #/home/$WWW_USER/.k/runtimes/kre-mono.1.0.0-beta3/bin/k
APPROOT=<PATH_TO_APPLICATION> #/home/$WWW_USER/vNext/mvc/HelloMvc

PIDFILE=$APPROOT/kestrel.pid
LOGFILE=$APPROOT/kestrel.log

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

start() {
  if [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE); then
    echo 'Service already running' >&2
    return 1
  fi
  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
  fi
  echo 'Stopping service...' >&2
  start-stop-daemon -K -p "$PIDFILE"
  rm -f "$PIDFILE"
  echo 'Service stopped' >&2
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
esac

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.