Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to create a daemon for dotnet under Linux

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to create daemons for dotnet under Linux". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

What is a daemon

In linux or unix operating systems, a Daemon is a special process that runs in the background, which is independent of the control terminal and periodically performs certain tasks or waits for certain events to be processed. Because in linux, every interface that the system communicates with the user is called the terminal, every process running from the terminal will be attached to this terminal, this terminal is called the control terminal of these processes, when the control terminal is closed, the corresponding process will automatically shut down. But the daemon can break through this restriction, it is separated from the terminal and runs in the background, and the purpose of leaving the terminal is to prevent the information of the process from being displayed in any terminal and the process will not be interrupted by the terminal information generated by any terminal. It runs from the time it is executed and does not exit until the entire system is shut down.

The creation daemon here refers to the creation of a daemon by the host process of the dotnet xxx.dll command issued to the asp.net core program on Linux.

There are many tools for managing processes on Linux, and we use Supervisor to do this.

There are two reasons:

1. It is recommended by Microsoft official documentation to reduce the cost of learning.

2. It is not necessarily the best, but it must be the most well documented.

Supervisor introduction

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

Official document: http://supervisord.org/

Supervisor installation

You can install it directly using the brew tool in masOS:

Brew install supervisor

Use the following command to install in linux:

Ubuntu

Sudo apt-get install supervisor

Centos

Yum install supervisor

Python

Pip install supervosor

Easy_install supervisor

After the installation is complete:

Mac:~ yangxiaodong$ brew install supervisorWarning: supervisor-3.2.1 already installedSupervisor configuration, common command

After the installation is complete, create a new configuration file (touch HelloWebApp.conf) in the / ect/supervisor/conf.d/ directory and name it HelloWebApp.conf

Open HelloWebApp.conf (vim HelloWebApp.conf) and write the following command:

[program:HelloWebApp] command=dotnet HelloWebApp.dll # commands to be executed environment=ASPNETCORE__ENVIRONMENT=Production # environment variables user=www-data # user identity executed by the process stopsignal=INTautostart=true # whether to automatically start autorestart=true # whether to automatically restart startsecs=1 # automatic restart interval stderr_logfile=/var/log/HelloWebApp.err.log # standard error log stdout _ logfile=/var/log/HelloWebApp.out.log # standard output log

After the configuration (: wq save exit), you need to reload the configuration.

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

Or you can restart Supervisor directly:

Sudo service supervisor stopsudo service supervisor start

If you report an error at startup, you can open the file located in / etc/log/supervisor/supervisord.log 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

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

Open the browser and type http://localhost:5000 to find that you are ready to browse.

Supervisor common commands

Supervisorctl shutdown # close all tasks supervisorctl stop | start program_namesupervisorctl status # View the status of all tasks Supervisor UI console

Supervisor provides us with a graphical interface to manage processes and tasks by default, which is configured by default in macOS, but we need to open it manually in Linux.

Open the file located in / etc/supervisor/supervisord.conf and add an inet_http_server node

Then you can view the running process through the interface:

Test it

Finally, let's test whether it will restart automatically and run automatically when powered on.

1. Kill dot net in process management and find that it can be restarted. The following is the log:

2016-07-09 12 INFO spawned: 'HelloWebApp' with pid 17742016-07-09 12 INFO success 24 INFO success: HelloWebApp entered RUNNING state, process has stayed up for > than 1 seconds (startsecs) 2016-07-09 12 27 expected 43208 INFO exited: HelloWebApp (exit status 0; expected) 2016-07-09 1227 expected: 44223 INFO spawned:' HelloWebApp' with pid 36872016-07-09 12 seconds 27243 INFO success: HelloWebApp entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)

2. Restart the machine and find that it can run automatically.

This is the end of "how to create daemons for dotnet under Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report