stable-diffusion-webui 检测服务

stable-diffusion-webui 检测服务

在使用stable-diffusion-webui的时候,偶发性的会出现进程卡死的状况,这里我们用一个检测脚本来确保它稳定运行。

进程运行状态检测

#!/bin/bash

# 函数:查找并杀掉运行在指定目录的python3进程
kill_process_in_dir() {
    local target_process=$1
    local target_dir=$2

    # 使用pgrep获取所有python3进程的PID
    pids=$(pgrep -f python3)

    for pid in $pids; do
        # 使用pwdx找到进程的实际运行目录
        dir=$(pwdx $pid 2>/dev/null | awk '{print $2}')
        
        # 如果目录匹配,则杀掉该进程
        if [ "$dir" == "$target_dir" ]; then
            echo "找到匹配的进程,PID: $pid,正在杀掉..."
            kill -9 $pid
            break  # 假设只有一个进程匹配,所以杀掉后就退出循环
        fi
    done
}

kill_by_dir_and_cmd() {
    local target_dir=$1
    local target_cmd=$2

    # 使用ps获取所有进程的详细信息
    ps -ef | grep "$target_cmd" | grep -v grep | while read -r user pid rest; do
        # 使用pwdx找到进程的实际运行目录
        dir=$(pwdx $pid 2>/dev/null | awk '{print $2}')
        
        # 如果目录和命令都匹配,则杀掉该进程
        if [ "$dir" == "$target_dir" ] && [[ $rest == *"$target_cmd"* ]]; then
            echo "找到匹配的进程,PID: $pid,正在杀掉..."
            kill -9 $pid
        fi
    done
}

# 无限循环,周期性地检测应用状态
while true; do
    # 使用curl检查应用的运行状态
    response=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:7860)

    # 如果响应是40x或50x,那么重启应用
    if [[ $response == 4* ]] || [[ $response == 5* ]]; then
        echo "应用运行异常,正在重新启动..."

        #方法一 调用函数,查找并杀掉运行在指定目录的python3进程
        # kill_process_in_dir "python3" "/home/sd/stable-diffusion-webui"
        
        #方法二 调用函数,查找并杀掉指定运行目录和命令的进程
        kill_by_dir_and_cmd "/home/sd/stable-diffusion-webui/" "webui.sh"

        # 重新启动应用
        /home/sd/stable-diffusion-webui/webui.sh &

        echo "应用已重新启动"
    else
        echo "应用运行正常"
    fi

    # 暂停1分钟后再次检测
    sleep 60
done

将检测脚本配置为系统服务

要将前面的检测脚本变成一个Ubuntu系统服务,并使其在系统启动时自动运行,可以使用systemd。以下是如何创建一个systemd服务的步骤:

  1. 创建一个新的systemd服务文件:

    创建一个新的文件 /etc/systemd/system/webui-monitor.service

    sudo vi /etc/systemd/system/webui-monitor.service
    
  2. 在服务文件中添加以下内容:

    [Unit]
    Description=WebUI Monitor Service
    After=network.target
    
    [Service]
    ExecStart=/path/to/your/script.sh
    Restart=always
    User=your_username
    Group=your_groupname
    Environment=PATH=/usr/bin:/usr/local/bin
    WorkingDirectory=/path/to/script/directory
    
    [Install]
    WantedBy=multi-user.target
    
    • 替换 /path/to/your/script.sh 为脚本的完整路径。
    • 替换 your_usernameyour_groupname 为运行此脚本的用户和组。
    • 替换 /path/to/script/directory 为脚本所在的目录。
  3. 为脚本赋予执行权限:

    sudo chmod +x /path/to/your/script.sh
    
  4. 启动并启用服务:

    使用以下命令启动服务:

    sudo systemctl start webui-monitor.service
    

    使用以下命令使服务在启动时自动运行:

    sudo systemctl enable webui-monitor.service
    
  5. 其他有用的命令:

    • 检查服务状态: sudo systemctl status webui-monitor.service
    • 停止服务: sudo systemctl stop webui-monitor.service
    • 重新启动服务: sudo systemctl restart webui-monitor.service
    • 查看服务日志: sudo journalctl -u webui-monitor.service

现在,每次Ubuntu系统启动时,webui-monitor服务都会自动运行,并周期性地检查WebUI应用的状态。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值