[Turn] Getting started with the installation and use of Linux Supervisor

Getting started with installing and using Linux Supervisor

 

       In linux or unix operating systems, a daemon is a special process running in the background, which is independent of the controlling terminal and periodically performs certain tasks or waits to process certain events. Because in linux, the interface where each system communicates with the user is called a terminal, and every process that starts from this terminal will be attached to this terminal. This terminal is called the controlling terminal of these processes. When the controlling terminal is closed , the corresponding process will be automatically closed. But the daemon process can break through this limitation, it is separated from the terminal and runs in the background, and the purpose of it is separated from the terminal to avoid the information of the process running in any terminal and the process will not be displayed by any terminal. interrupted by the resulting terminal message. It starts running when it is executed and does not exit until the entire system shuts down.

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

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

1. It is recommended by Microsoft's official documents to reduce learning costs.

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

        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 lot of functions to manage processes.

Official documentation: http://supervisord.org/

 

There are currently three problems

Question 1: The ASP.NET Core application runs in the shell. If you close the shell, you will find that the ASP.NET Core application is closed, making the application inaccessible. Of course, we don't want to encounter this situation, and the production environment is not There is zero tolerance for this situation. 

Question 2: If the ASP.NET Core process terminates unexpectedly, it is necessary to manually connect to the shell to restart it, which is often not timely enough. 

Question 3: If the server is down or needs to be restarted, we still need to connect to the shell to start.

 

In order to solve this problem, we need to have a program to monitor the status of the ASP.NET Core application. Restart as soon as the application stops running.

 

The operation is as follows:

1. Install Supervisor

Execute the following commands:

  1. yum install python-setuptools
  2. easy_install supervisor

or

  1. If easy_install does not work well, download it from the official website: wget https://pypi.python.org/packages/80/37/964c0d53cbd328796b1aeb7abea4c0f7b0e8c7197ea9b0b9967b7d004def/supervisor-3.3.1.tar.gz
  2. Then install via python:
  3. # tar zxf supervisor-3.3.1.tar.gz
  4. # cd monitor
  5. # python setup.py install

  If an error may be reported:

  1. Prompt that setuptools-0.6c11.tar is not installed,
    1. Download https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
    2. tar zxf setuptools-0.6c11.tar.gz
    3. cd setuptools-0.6c11/
    4. python setup.py build
    5. python setup.py  install
  2. Prompt download error, need meld3>0.6.5
    1. Download http://dl.fedoraproject.org/pub/epel/7/x86_64/p/python-meld3-0.6.10-1.el7.x86_64.rpm
    2. install rpm -ivh python-meld3-0.6.10-1.el7.x86_64.rpm

  The following prompts, the installation is complete:

1
2
Using /usr/lib64/python2.7/site-packages
Finished processing dependencies  for  supervisor==3.3.1

  

 

2.  Configure Supervisor

a. Create folders and profiles

mkdir /etc/supervisor

echo_supervisord_conf > /etc/supervisor/supervisord.conf

 

b. Modify the content of the /etc/supervisor/supervisord.conf file

At the end of the file at the [include] node

Change ;files=relative/directory/*.ini to files=conf.d/*.comf

save and exit

 

c. Execute the supervisorctl reload command to make the configuration file take effect.

d. Create a conf.d folder under /etc/supervisor/ , and ProjectName.conf ( named after the project name)

e. Open the ProjectName.conf file and add the following:

[program: ProjectName]

command=dotnet ProjectName.dll ; command to run the program

directory=/root/Publishing/PublishOutput/ ; the directory where the command is executed

autorestart=true ; Whether the program will automatically restart if the program exits unexpectedly

autostart=true ; whether to start automatically

stderr_logfile=/var/log/ProjectName.err.log ; error log file

stdout_logfile=/var/log/ProjectName.out.log ; output log file

environment=ASPNETCORE_ENVIRONMENT=Production ; Process environment variables

user=root ; the user identity of the process execution

stopsignal = INT

startsecs=1 ; auto restart interval

 

save and exit

 

3.  Run supervisord to see if it takes effect, and execute the following command:

supervisord -c /etc/supervisor/ supervisord.conf

ps -ef | grep ProjectName

 

return

root     27007 27006  1 13:21 ?        00:00:02 dotnet ProjectName.dll                                                                                                                  

root     27026 26810  0 13:23 pts/0    00:00:00 grep --color=auto ProjectName

Indicates that the operation was successful!

Browser access site...

 

Note: When the following prompt appears when executing the first command:

Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord.

For help, use /usr/bin/supervisord –h

It is because there is an application that is configured with supervisor running, you need to execute the supervisorctl shutdown command to terminate it, or recreate a ProjectName.conf file and execute the first command .

 

4.  Common commands

1
2
3
4
5
6
7
8
9
sudo service supervisor stop 停止supervisor服务
 
sudo service supervisor start 启动supervisor服务
 
supervisorctl shutdown #关闭所有任务
 
supervisorctl stop|start program_name #启动或停止服务
 
supervisorctl status #查看所有任务状态

   

5、 配置supervisord开机启动

a.在指定目录下创建文件supervisord.service

vim /usr/lib/systemd/system/supervisord.service

 

b.输入以下内容:

[Unit]

Description=Supervisor daemon

 

[Service]

Type=forking

ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

ExecStop=/usr/bin/supervisorctl shutdown

ExecReload=/usr/bin/supervisorctl reload

KillMode=process

Restart=on-failure

RestartSec=42s

 

[Install]

WantedBy=multi-user.target

 

保存并退出

 

执行以下命令:

systemctl enable supervisord

提示:

Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

 

验证是否为开机启动:

systemctl is-enabled supervisord

提示:

enabled

表示设置成功!

至此,创建supervisor守护进程完毕。

 

参考:

Supervisor的安装与使用入门

CentOS 6.4安装 Python2.7.10

将ASP.NET Core应用程序部署至生产环境中(CentOS7)

 

转自:http://www.cnblogs.com/Hai--D/p/5820718.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326863521&siteId=291194637