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

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

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

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

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

bash 复制代码
mkdir dotnet-monitor.sh

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

bash 复制代码
#!/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; then
        log "应用已启动"
        return 0
    else
        log "启动失败"
        return 1
    fi
}

# 函数:停止应用
stop_app() {
    log "停止应用: $APP_NAME"
    pkill -f "dotnet.*$APP_DLL"
    sleep 3
}

# 主循环
restart_count=0
last_restart=$(date +%s)

log "启动,监控: $APP_NAME"

while true; do
    if ! is_running; then
        log "应用未运行,尝试重启..."

        # 检查是否在冷却期内(防频繁重启)
        current_time=$(date +%s)
        time_since_last=$((current_time - last_restart))

        if [ $restart_count -ge $MAX_RESTARTS ] && [ $time_since_last -lt $RESTART_COOLDOWN ]; then
            log "重启次数过多,进入冷却期..."
            sleep $RESTART_COOLDOWN
            restart_count=0
            last_restart=$(date +%s)
        else
            stop_app
            if start_app; then
                restart_count=$((restart_count + 1))
                last_restart=$(date +%s)
            else
                log "启动失败,等待下次检查..."
            fi
        fi
    else
        log "应用正在运行"
    fi

    sleep $CHECK_INTERVAL
done

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

bash 复制代码
sudo chmod +x /opt/dotnet-monitor.sh

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

bash 复制代码
vi /etc/systemd/system/dotnet-monitor.service

添加如下内容:

bash 复制代码
[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.启动服务:

bash 复制代码
sudo systemctl daemon-reexec
sudo systemctl enable dotnet-monitor.service
sudo systemctl start dotnet-monitor.service

ok

相关推荐
M1582276905519 小时前
四通道全能组网!SG-Canet-410 CAN转以太网网关,破解工业CAN通信瓶颈
linux·运维·服务器
誰能久伴不乏19 小时前
【Qt实战】工业级多线程串口通信:从底层协议设计到完美收发闭环
linux·c++·qt
bjxiaxueliang19 小时前
一文解决蓝牙连接难题:Ubuntu命令行蓝牙强制配对
linux·ubuntu·蓝牙连接命令
浪客灿心20 小时前
Linux库制作与原理
linux·运维·服务器
bantinghy20 小时前
Nginx基础加权轮询负载均衡算法
服务器·算法·nginx·负载均衡
成为你的宁宁20 小时前
【Linux Swap 交换分区:定义、作用与操作指南】
linux·交换分区
Dontla20 小时前
Vite代理 vs Nginx代理(开发环境用Vite,生产环境用Nginx)
运维·nginx
运维小欣20 小时前
博睿数据:以Agentic AI驱动智能运维未来
运维·人工智能
Ha_To21 小时前
2026.1.28 docker安装
运维·docker·容器
祁鱼鱼鱼鱼鱼21 小时前
rhce-shell条件测试
linux·运维