当前位置: 首页 > ops >正文

.NET 应用程序 Linux下守护进程脚本编写

下面的脚本是生产可用,可靠的sh脚本,用于监控 .NET 应用程序并自动重启。

假如你打包发布到Linux的程序名称为MyAspDemo;

推荐打包模式为框架依赖:需要在Linux上安装对应的donet版本;

1.在Linux下新建一个文件,如:

mkdir dotnet-monitor.sh

如上新建了一个名为 dotnet-monitor.sh的脚本文件,打开脚本文件,添加如下内容:

#!/bin/bash# 配置
APP_NAME="MyAspDemo"                           # 应用名称
APP_DIR="/opt/services/publish"                   # 应用所在目录
APP_DLL="MyAspDemo.Api.dll"                        # 主程序集
DOTNET_CMD="dotnet"                        # dotnet 命令
LOG_FILE="/var/log/dotnet-monitor.log"     # 日志文件
CHECK_INTERVAL=30                          # 检查间隔(秒)
MAX_RESTARTS=5                             # 最大重启次数(防崩溃循环)
RESTART_COOLDOWN=60                        # 重启冷却时间(秒)# 函数:记录日志
log() {echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}# 函数:检查应用是否在运行
is_running() {pgrep -f "dotnet.*$APP_DLL" > /dev/null
}# 函数:启动应用
start_app() {log "启动应用: $APP_NAME"cd "$APP_DIR" || { log "无法进入目录: $APP_DIR"; return 1; }nohup $DOTNET_CMD "$APP_DLL" > /dev/null 2>&1 &sleep 5  # 等待进程启动if is_running; thenlog "应用已启动"return 0elselog "启动失败"return 1fi
}# 函数:停止应用
stop_app() {log "停止应用: $APP_NAME"pkill -f "dotnet.*$APP_DLL"sleep 3
}# 主循环
restart_count=0
last_restart=$(date +%s)log "启动,监控: $APP_NAME"while true; doif ! is_running; thenlog "应用未运行,尝试重启..."# 检查是否在冷却期内(防频繁重启)current_time=$(date +%s)time_since_last=$((current_time - last_restart))if [ $restart_count -ge $MAX_RESTARTS ] && [ $time_since_last -lt $RESTART_COOLDOWN ]; thenlog "重启次数过多,进入冷却期..."sleep $RESTART_COOLDOWNrestart_count=0last_restart=$(date +%s)elsestop_appif start_app; thenrestart_count=$((restart_count + 1))last_restart=$(date +%s)elselog "启动失败,等待下次检查..."fifielselog "应用正在运行"fisleep $CHECK_INTERVAL
done

2.为刚刚创建的脚本文件添加执行权限:

sudo chmod +x /opt/dotnet-monitor.sh

3.创建systemd service文件,如下:

vi /etc/systemd/system/dotnet-monitor.service

添加如下内容:

[Unit]
Description=DotNet Monitor
After=network.target[Service]
Type=simple    
User=www-admin    //自定义名称
WorkingDirectory=/opt
ExecStart=/opt/service/dotnet-monitor.sh  //脚本所在目录
Restart=always
RestartSec=10[Install]
WantedBy=multi-user.target

4.启动服务:

sudo systemctl daemon-reexec
sudo systemctl enable dotnet-monitor.service
sudo systemctl start dotnet-monitor.service

ok

http://www.xdnf.cn/news/17657.html

相关文章:

  • 基于.Net Framework4.5 Web API 引用Swagger
  • JavaWeb核心:HttpServletRequest与HttpServletResponse详解
  • mac环境下安装git并配置密钥等
  • 从行业场景到视觉呈现:3ds Max 与 C4D 效果图的本质分野
  • Pycharm现有conda环境有对应env,但是添加后没反应
  • 学习嵌入式的第十九天——Linux——文件编程
  • Spring Boot 使用 @NotBlank + @Validated 优雅校验参数
  • 疯狂星期四文案网第38天运营日记
  • TorchDynamo - API
  • 互联网大厂Java求职面试实录:Spring Boot到微服务与AI的技术问答
  • 【Unity开发】Unity核心学习(一)
  • 如何在 Ubuntu 24.04 LTS Noble Linux 上安装 FileZilla Server
  • MyBatis 中 XML 与 DAO 接口的位置关系及扫描机制详解
  • react与vue的对比,来实现标签内部类似v-for循环,v-if等功能
  • 万字详解C++11列表初始化与移动语义
  • 如何把ubuntu 22.04下安装的mysql 8 的 数据目录迁移到另一个磁盘目录
  • 基于深度学习的苹果品质智能检测算法研究
  • Kubernetes(K8S)中,kubectl describe node与kubectl top pod命令显示POD资源的核心区别
  • .net\c#web、小程序、安卓开发之基于asp.net家用汽车销售管理系统的设计与实现
  • Android Activity 的对话框(Dialog)样式
  • LaTeX(排版系统)Texlive(环境)Vscode(编辑器)环境配置与安装
  • PostgreSQL——索引
  • SpringBoot工程妙用:不启动容器也能享受Fat Jar的便利
  • Redis:是什么、能做什么?
  • 第十三节:后期处理:效果增强
  • MySQL优化常用的几个方法
  • 使用 Python Selenium 和 Requests 实现歌曲网站批量下载实战
  • 100、【OS】【Nuttx】【构建】cmake 配置保存
  • 文心4.5专家负载均衡机制深度解析
  • 【Virtual Globe 渲染技术笔记】4 椭球面上的曲线