在linux中使用Supervisor部署.net core 定时任务

使用PeterKottas.DotNetCore.WindowsService开发的定时任务,之前一直部署在windows服务器下面。最近开始迁移至linux环境下(centOS 7.6),这里使用Supervisor进行部署,也可以使用docker哦。接下来记录了本次安装部署的全过程(踩坑过程)。

1、安装Supervisor

yum install python-setuptools
       easy_install supervisor
       查看版本:supervisord -v

 

2、配置Supervisor

mkdir /etc/supervisor 
       echo_supervisord_conf > /etc/supervisor/supervisord.conf 
       #supervisor的指定配置文件 
       supervisord -c /etc/supervisor/supervisord.conf 
       #编辑配置 
      vim /etc/supervisor/supervisord.conf

这个配置需要需要修改以下几处地方
        1、在文件的末尾加上以下两行;conf.d/*.conf是conf.d文件夹下面以.conf结尾的配置文件,用来配置.net core系统
             [include] 
             files=conf.d/*.conf

2、将末尾处的两行注释掉;不然后面可能会报错找不到.ini文件

      #;[include]
             #;files = relative/directory/*.ini

3、修改几处路径带/tmp/的配置点;不然后面会报错找不到文件:unix:///tmp/supervisor.sock no such file
                        file=/tmp/supervisor.sock   ; the path to the socket file
             改成:file=/var/run/supervisor.sock   ; the path to the socket file
                        logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
             改成:logfile=/var/log/supervisor/supervisord.log ; main log file; default $CWD/supervisord.log
                        pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
             改成:pidfile=/var/run/supervisord.pid ; supervisord pidfile; default supervisord.pid
                        serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
             改成:serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket [注:需要与第一条的一样/var/run]

3、Supervisor开机启动

新建文件并添加以下内容:vim supervisord.service

# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[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

将文件拷贝至:mv supervisord.service /usr/lib/systemd/system/supervisord.service

执行命令:systemctl enable supervisord

4、配置.net core系统配置

新增配置文件(要注意文件权限的问题):vim /etc/supervisor/conf.d/xxxxxxx.conf

其中/data/site/host.dll是.net core系统的主入口文件。program后面的host.dll是程序名称后面需要用到的

[program:host.dll]
command=dotnet /data/site/host.dll 运行程序的命令
directory=/data/site/
autorestart=true  程序意外退出是否自动重启
autostart=true  是否自动启动
stderr_logfile=/data/site/log/err.log 输出日志文件
stdout_logfile=/data/site/log/out.log  错误日志文件
environment=ASPNETCORE_ENVIRONMENT=Development 进程环境变量
user=root 进程执行的用户身份
stopsignal=INT
startsecs=1  自动重启间隔 

5、最后

supervisorctl reload       重新启动配置中的所有程序  
supervisorctl update       更新新的配置到supervisord
supervisorctl              打开supervisor控制终端
supervisorctl status       查看进程列表       
supervisorctl start program_name  启动某个进程(program_name=你配置中写的程序名称)    
supervisorctl stop program_name   停止某一进程 (program_name=你配置中写的程序名称)      
supervisorctl restart program_name   重启某一进程 (program_name=你配置中写的程序名称)     
supervisorctl stop all   关闭所有进程

以上用到的文件夹可能需要自己创建好。文件会自动创建。

如果报错:error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224

则执行命令:/usr/bin/python2 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
然后再重新:supervisorctl update
                     supervisorctl host
                     supervisorctl start host.dll

发布了65 篇原创文章 · 获赞 28 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_26900081/article/details/100746568
今日推荐