Running DNX/Kestrel via a Launch Daemon on OSX
Author: Joe
Date: 02/01/2016 14:05:00
While .NET Core is meant to help .NET developers implement cross-platform solutions, there's not a whole lot of documentation about how to get your ASP.NET Core websites up and running as a daemon service.
These are the steps I performed to start my Kestrel website on system start-up. They may be more than what's needed, but what can you expect? Setting up ASP.NET websites on platforms other than Windows is uncharted waters. :-)
What you'll need to do is the following:
- Set up your DNVM/DNU/DNX environment via brew.
- Set up a user account in System Preferences -> Users and Groups. This account will be the account your daemon runs under. In this example, it'll be "testuser".
- Create the folder "/usr/local/netcore" and grant the "testuser" account read/write/execute access (you can lock down access later as needed.)
- Add the following file into /Library/LaunchDaemons. Modify the Label and UserName values to reflect your own site. Save the file with the same name as your label, but with ".plist" appended (i.e. "com.nuts4net.testsite.plist") This will tell OSX's launchd process to run your service on startup, and restart it in the event it crashes.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.nuts4net.testsite</string> <key>UserName</key> <string>testuser</key> <key>ProgramArguments</key> <array> <string>/bin/bash</string> <string>/usr/local/netcore/testsite/run-site.sh</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> </dict> </plist>
- Deploy your web code to the /usr/local/netcore folder. In the plist example above, my code sits in "testsite".
- Create a script called "run-site.sh" with the following contents, place it in /usr/local/netcore/testsite, make it executable by yourself and root:
source /usr/local/Cellar/dnvm/1.0.0-dev/bin/dnvm.sh cd /usr/local/netcore/testsite dnx web
- Open up a terminal window, and type the following:
sudo launchctl load -w /Library/LaunchDaemons/org.nuts4net.testsite
- Open up the Console app, and filter to "org.nuts4net.testsite". If everything worked, you should see the initial start-up of the script. If you see issues about the script failing, and re-starting in 10 seconds, execute the following command in your terminal window to stop the service so you can investigate:
sudo launchctl load -w /Library/LaunchDaemons/org.nuts4net.testsite
Let me know in the comments if you run into any issues, notice something I missed, or have improvements to this process. I'm by no means an OSX guy...just know enough to be dangerous with it. ;-)