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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 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