惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
O
OpenAI News
Jina AI
Jina AI
腾讯CDC
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
WordPress大学
WordPress大学
F
Full Disclosure
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
www.infosecurity-magazine.com
www.infosecurity-magazine.com
SecWiki News
SecWiki News
F
Fortinet All Blogs
C
Check Point Blog
H
Hacker News: Front Page
H
Help Net Security
G
Google Developers Blog
The Cloudflare Blog
Google Online Security Blog
Google Online Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
A
About on SuperTechFans
S
Secure Thoughts
U
Unit 42
L
LINUX DO - 最新话题
博客园 - 【当耐特】
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
TaoSecurity Blog
TaoSecurity Blog
Recorded Future
Recorded Future
Apple Machine Learning Research
Apple Machine Learning Research
AI
AI
aimingoo的专栏
aimingoo的专栏
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Heimdal Security Blog
Y
Y Combinator Blog
月光博客
月光博客
Hacker News: Ask HN
Hacker News: Ask HN
D
DataBreaches.Net
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Schneier on Security
Schneier on Security
Cisco Talos Blog
Cisco Talos Blog
Last Week in AI
Last Week in AI

博客园 - uestc2007

AI 智能体项目的测试 vllm方式部署Deepseek R1、Embedding、Reranker、Qwen 磁盘挂载 LibreOffice 批量将.doc文件转换为.docx Supervisor 监控 Python 服务 资源监控脚本 Docker 安全及日志管理 Docker资源控制 做项目管理需要哪些技能 项目管理的基本工作是什么 知识图谱介绍(三) 知识图谱介绍(二) 知识图谱介绍(一) 安装Kuboard管理k8s Registry&Harbor私有仓库构建 EMQX配置用户名和密码开启emqx_auth_mnesia认证方式连接 开关量、数字量、模拟量、离散量和脉冲量它们之间有什么区别? Kubernetes基础总结 Kubernetes管理应用程序、服务常用命令、集群监视
容器异常或停止自动启动脚本
uestc2007 · 2025-12-17 · via 博客园 - uestc2007

方案1:使用 systemd 监控(推荐)
1.1 创建 systemd 服务
创建/etc/systemd/system/nginx-container.service,信息如下:

vi /etc/systemd/system/nginx-container.service,信息如下:

[Unit]
Description=Nginx Docker Container
Requires=docker.service
After=docker.service
Wants=network-online.target

[Service]
Type=forking
Restart=always
RestartSec=10
TimeoutStartSec=0
ExecStart=/usr/local/bin/start-nginx-container
#ExecStop=/usr/bin/docker stop -t 10 nginx

[Install]
WantedBy=multi-user.target

1.2 创建启动脚本
创建/usr/local/bin/start-nginx-container,信息如下:

vi /usr/local/bin/start-nginx-container,信息如下:

#!/bin/bash

CONTAINER_NAME="nginx"
COMPOSE_DIR="/root/nginx"

echo "监控容器: $CONTAINER_NAME"


# 检查容器是否在运行(docker ps 查看运行中的容器)
if ! docker ps --format '{{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
        echo "$(date): 容器 $CONTAINER_NAME 未运行"

        # 先尝试启动已有容器
        if docker start "$CONTAINER_NAME" 2>/dev/null; then
                echo "$(date): 成功启动已有容器"
        else
                echo "$(date): 没有找到容器,尝试使用docker-compose重新创建"
                cd "$COMPOSE_DIR" && docker-compose up -d
        fi
fi

赋予执行权限:
chmod +x /usr/local/bin/start-nginx-container

1.3 启用服务

systemctl daemon-reload
systemctl enable nginx-container.service
systemctl start nginx-container.service

1.4 使用方法

# 查看服务状态
sudo systemctl status nginx-container

# 查看日志
sudo journalctl -u nginx-container -f

# 手动重启服务
sudo systemctl restart nginx-container

# 停止服务(这会停止监控,但容器可能还在运行)
sudo systemctl stop nginx-container

# 禁用开机启动
sudo systemctl disable nginx-container

1.5改进脚本

#一、 创建监控脚本
sudo tee /usr/local/bin/monitor-nginx-container > /dev/null << 'EOF'
#!/bin/bash

CONTAINER_NAME="nginx"
COMPOSE_DIR="/root/nginx"
CHECK_INTERVAL=10

trap "echo '监控进程收到信号,退出...'; exit 0" SIGTERM SIGINT

echo "开始监控容器: $CONTAINER_NAME"

check_and_start() {
    if ! docker ps --format '{{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S'): 容器 $CONTAINER_NAME 未运行"
        
        if docker start "$CONTAINER_NAME" 2>/dev/null; then
            echo "$(date '+%Y-%m-%d %H:%M:%S'): 成功启动已有容器"
            return 0
        else
            echo "$(date '+%Y-%m-%d %H:%M:%S'): 尝试使用 docker-compose 重新创建"
            if cd "$COMPOSE_DIR" && docker-compose up -d 2>&1; then
                echo "$(date '+%Y-%m-%d %H:%M:%S'): 容器创建成功"
                return 0
            else
                echo "$(date '+%Y-%m-%d %H:%M:%S'): 容器创建失败"
                return 1
            fi
        fi
    fi
    return 0
}

check_and_start

while true; do
    for ((i=0; i<CHECK_INTERVAL; i++)); do
        sleep 1
    done
    
    if ! docker ps --format '{{.Names}}' | grep -q "^$CONTAINER_NAME$"; then
        echo "$(date '+%Y-%m-%d %H:%M:%S'): 检测到容器停止,尝试重新启动..."
        check_and_start
    fi
done
EOF

# 赋予执行权限
sudo chmod +x /usr/local/bin/monitor-nginx-container

#二、 创建 systemd 服务
sudo tee /etc/systemd/system/nginx-container.service > /dev/null << 'EOF'
[Unit]
Description=Nginx Docker Container Monitor
Requires=docker.service
After=docker.service
Wants=network-online.target

[Service]
Type=simple
Restart=always
RestartSec=10
StartLimitInterval=0
User=root
Group=root
StandardOutput=journal
StandardError=journal
ExecStart=/usr/local/bin/monitor-nginx-container
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGTERM
TimeoutStopSec=30

[Install]
WantedBy=multi-user.target
EOF

# 重新加载 systemd 并启用服务
sudo systemctl daemon-reload
sudo systemctl enable nginx-container.service
sudo systemctl start nginx-container.service

# 检查服务状态
sudo systemctl status nginx-container.service