运维需求:获取服务器的运行情况,是否CPU、内存较高等,上报到运维系统
环境:ubuntu16.04
工具::netcore2.1、supervisor
程序实现(代码就不贴了)参考:https://blog.csdn.net/u014518337/article/details/86291984
因需要获取系统数据,部分命令行不支持在docker环境运行,因此直接由dotnet直接运行程序,守护进程由supervisor 守护
1、安装netcore2.1
wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb sudo apt-get install apt-transport-https sudo apt-get update sudo apt-get install dotnet-sdk-2.1
执行 dotnet --info 查看是否安装正确
2、安装supervisor
参考:https://www.cnblogs.com/savorboard/p/dotnetcore-supervisor.html
sudo apt-get install supervisor
安装完成之后,在 /ect/supervisor/conf.d/
目录下新建一个配置文件(touch HelloWebApp.conf
),取名为 HelloWebApp.conf
打开HelloWebApp.conf (vim HelloWebApp.conf
),写入如下命令:
[program:HelloWebApp] command=dotnet HelloWebApp.dll #要执行的命令 directory=/home/xxx/publish #命令执行的目录 environment=ASPNETCORE__ENVIRONMENT=Production #环境变量 user=www-data #进程执行的用户身份 stopsignal=INT autostart=true #是否自动启动 autorestart=true #是否自动重启 startsecs=1 #自动重启间隔 stderr_logfile=/var/log/HelloWebApp.err.log #标准错误日志 stdout_logfile=/var/log/HelloWebApp.out.log #标准输出日志
注意:上面代码需要把注释 #后面的中文删掉
修改配置文件/etc/supervisor/supervisord.conf 添加supervisord web管理功能 红色为新增配置部分
;upervisor config file [unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) chmod=0700 ; sockef file mode (default 0700) [inet_http_server] port=*:9001 username=user password=123 [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) ; the below section must remain in the config file for RPC ; (supervisorctl/web interface) to work, additional interfaces may be ; added by defining them in separate rpcinterface: sections [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket ; The [include] section can just contain the "files" setting. This ; setting can list multiple files (separated by whitespace or ; newlines). It can also contain wildcards. The filenames are ; interpreted as relative to this file. Included files *cannot* ; include files themselves. [include] files = /etc/supervisor/conf.d/*.conf
保存之后重启下程序
sudo supervisorctl shutdown && sudo supervisord -c /etc/supervisor/supervisord.conf
如果启动异常根据提示查看配置修改配置文件