Create daemon for dotnet in ASP. NET Core Linux (required knowledge)

Source: Internet
Author: User
Tags dot net

Create daemon for dotnet in ASP. NET Core Linux (required knowledge)
Preface

In the previous article, we introduced how to deploy our asp.net core application in the Docker container. This article mainly describes how to create a daemon process for the dotnet program we deploy in Linux or macOs, to ensure that our program can still be accessed normally when an exception occurs or the computer restarts.

If you plan to use asp.net core for project development and deploy the program on Linux in the future, this article is worth your favorites.

Directory
  • What is a daemon?
  • Supervisor Introduction
  • Supervisor Installation
  • Supervisor configuration, Common commands
  • Supervisor UI Console
What is a daemon?

In linux or unix operating systems, Daemon is a special process running on the background, it is independent of the control terminal and periodically executes a task or waits to process some events. In linux, the interface for communication between each system and users is called a terminal. Every process starting from this terminal is attached to this terminal, which is called the control terminal of these processes, when the control terminal is closed, the corresponding process is automatically closed. However, the daemon can break through this restriction. It is isolated from the terminal and runs in the background, in addition, it leaves the terminal to prevent information in the process of running from being displayed in any terminal and the process will not be interrupted by terminal information generated by any terminal. It runs from the execution time until the entire system is shut down.

The creation of the daemon here refers to the release of the asp.net core program on Linux.dotnet xxx.dllCommand to create a daemon.

There are many tools on Linux that can manage processes. We use Supervisor to do this.
There are two reasons:
1. It is recommended by Microsoft official documentation to reduce learning costs.
2. It is not necessarily the best, but it must be the most comprehensive document.

Supervisor Introduction

Supervisor is developed using Python (2.4 +). It is a Client/Server system that allows users to manage Unix system processes. It provides a large number of functions to manage processes.

Official documents: http://supervisord.org/

Supervisor Installation

Direct use in masOSbrewInstall the tool:
brew install supervisor

Run the following command in linux:

Ubuntu
sudo apt-get install supervisor

CentOS
yum install supervisor

Python
pip install supervosor
easy_install supervisor

After installation:

mac:~ yangxiaodong$ brew install supervisorWarning: supervisor-3.2.1 already installed
Supervisor configuration, Common commands

After the installation is complete/ect/supervisor/confg.d/Directory to create a configuration file (touch HelloWebApp.conf), NamedHelloWebApp.conf

Open HelloWebApp. conf (vim HelloWebApp.conf), Write the following command:

[Program: HelloWebApp] command = dotnet HelloWebApp. dll # command directory to be executed =/home/yxd/Workspace/publish # Command Execution directory environment = aspnetcore?environment = Production # environment variable user = www-data # process execution user identity stopsignal = INTautostart = true # Whether to automatically start autorestart = true # Whether to automatically restart startsecs = 1 # stderr_logfile =/var/log/HelloWebApp. err. log # stdout_logfile =/var/log/HelloWebApp. out. log # standard output log

After configuration (:wqSave and exit). You need to reload the configuration.

sudo supervisorctl shutdown && sudo supervisord -c /etc/supervisor/supervisord.conf

Or you can directly restart the Supervisor:

sudo service supervisor stopsudo service supervisor start

If an error is reported during startup/etc/log/supervisor/supervisord.logFile to view the specific log.

The log files output by the dotnet command are located in

/var/log/HelloWebApp.err.log/var/log/HelloWebApp.out.log

You can view the exception information or running information in the program in these files.

Open your browser and enterhttp://localhost:5000It is found that you can browse.

Common Supervisor commands

Supervisorctl shutdown # Close all tasks supervisorctl stop | start program_namesupervisorctl status # view the status of all tasks
Supervisor UI Console

By default, Supervisor provides us with a graphical interface for us to manage processes and tasks. The default configuration in macOS is as follows, but we need to manually enable it in Linux.

Open in/etc/supervisor/supervisord.confFile, add the inet_http_server Node

Then you can view the running process on the interface:

Test

Finally, let's test whether it will automatically restart and start automatically?

1. Destroy dot net in process management and restart it. The following are logs:

2016-07-09 12:24:18,626 INFO spawned: 'HelloWebApp' with pid 17742016-07-09 12:24:19,766 INFO success: HelloWebApp entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)2016-07-09 12:27:43,208 INFO exited: HelloWebApp (exit status 0; expected)2016-07-09 12:27:44,223 INFO spawned: 'HelloWebApp' with pid 36872016-07-09 12:27:45,243 INFO success: HelloWebApp entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

2. Restart the machine and run it automatically.

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.