run ASP.NET Core app under Linux on startup
This section in docs describes, how to create a service file to automatically start your Asp.Net Core app.
Create the service definition file:
sudo nano /etc/systemd/system/kestrel-hellomvc.service
The following is an example service file for the app:
[Unit]
Description=Example .NET Web API App running on Ubuntu
[Service]
WorkingDirectory=/var/aspnetcore/hellomvc
ExecStart=/usr/bin/dotnet /var/aspnetcore/hellomvc/hellomvc.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Development
[Install]
WantedBy=multi-user.target
Save the file and enable the service.
systemctl enable kestrel-hellomvc.service
Start the service and verify that it's running.
systemctl start kestrel-hellomvc.service
systemctl status kestrel-hellomvc.service
You need to set WorkingDirectory
- path to folder with your app and ExecStart
- with path to your app dll. By default this is enough.
From now, your app will automatically start on OS startup and will try to restart after crashes.
How to start asp.net core server on linux and keep it running
Basically what happens is:
- You login with ssh
- you startup an application under your user (
dotnet run
) - Close your ssh => logging out user, which means application is closed.
You need to start a service outside of your user. Here is some information:
- https://askubuntu.com/questions/8653/how-to-keep-processes-running-after-ending-ssh-session
Otherwise i'd advice you to ask on https://askubuntu.com/
Running a self-contained ASP.NET Core application on Ubuntu
Answer
Now, how do I run my application? My understanding is that because it is a self-contained .NET Core application I do not need to download and install .NET Core anything. My application should contain everything it needs.
You are correct. Run the executable.
When you create a self-contained app, the publish output "contains the complete set of files (both your app files and all .NET Core files) needed to launch your app." That includes the executable.
Example Self-Contained Deployment
Here is the output of dotnet publish -c release -r ubuntu.14.04-x64
for a simple self-contained application. Copy the publish directory to Ubuntu and run the executable.
C:\MyApp\bin\release\netcoreapp1.0\ubuntu.14.04-x64\publish\
...
libsos.so
libsosplugin.so
libuv.so
Microsoft.CodeAnalysis.CSharp.dll
Microsoft.CodeAnalysis.dll
Microsoft.CodeAnalysis.VisualBasic.dll
Microsoft.CSharp.dll
Microsoft.VisualBasic.dll
Microsoft.Win32.Primitives.dll
Microsoft.Win32.Registry.dll
mscorlib.dll
mscorlib.ni.dll
MyApp <------- On Ubuntu, run this executable
MyApp.deps.json and you will see Hello World!
MyApp.dll
MyApp.pdb
MyApp.runtimeconfig.json
sosdocsunix.txt
System.AppContext.dll
System.Buffers.dll
System.Collections.Concurrent.dll
System.Collections.dll
...
C:\MyApp\project.json
{
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": "1.0.1"
}
}
},
"runtimes": {
"ubuntu.14.04-x64" : {},
"win10-x64" : {}
}
}
C:\MyApp\Program.cs
public class Program
{
public static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
See Also
This document differentiates between framework-dependent and self-contained deployments.
How to run a .NET Core console application on Linux
Follow the below steps to run your application:
Publish your application as a self contained application:
dotnet publish -c release -r ubuntu.16.04-x64 --self-contained
Copy the
publish
folder to the Ubuntu machineOpen the Ubuntu machine terminal (CLI) and go to the project directory
Provide execute permissions:
chmod 777 ./appname
Execute the application
./appname
ASP.NET Core NodeServices on Azure Linux WebApp
After a long research and the assistance of Microsoft's Engineer https://github.com/caroe2014 this is the three steps final answer:
1) Startup.cs
services.AddNodeServices(options =>
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
options.ProjectPath = Path.GetFullPath("/home/site/wwwroot");
}
}
);
2) And what I found is Node is not present in the container so it is necessary to have a script to install and start it before starting the app itself. So I have this start1.cs file:
#!/bin/bash
apt-get install curl
curl -sL https://deb.nodesource.com/setup_12.x | bash
apt-get install -y nodejs
set -e
export PORT=8080
export ASPNETCORE_URLS=http://*:$PORT
dotnet "Web.Identity.dll"
Where Web.Identity.dll is the dll of my app.
3) Set startup command to /home/site/wwwroot/start1.sh (On Azure Portal - App service Configuration - or Azure DevOps).
That's all.
Related Topics
Why Using Pipe for Sort (Linux Command) Is Slow
Monitoring File and Directory Access on Linux
When "Vagrant Up" It Says "It Appears Your Machine Doesn't Support Nfs" (Debian Jessie)
How to Install Apxs Module on Apache 2.4.6
Difference Between Virtual Page and Page Frame
Concatenating Two String Variables in Bash Appending Newline
All Newlines Are Removed When Saving Cat Output into a Variable
Chmod - Protect Users' File Being Accessed So Only Owner Can Access
Sed Extracting Group of Digits
How to Use Make and Compile as C99
How to Chmod 0777 a File and Commit as Is to Git on Windows
Shell Shift Procedure - What Is This
Scp File from Local to Heroku Server
Is Lib{Library Name}.A/.So a Naming Convention for Static Libraries in Linux