动态监控U盘重启容器

需求背景

Ubuntu机器需要动态根据插入的U盘进行导入数据, 路径是约定为U盘内的固定路径.

但是服务是docker服务, 插入U盘并不会直接挂在到容器内部, 需要重启容器才能生效, 每次手动重启很麻烦, 自动检测U盘路径变化来操作容器.

配置动态监控脚本和服务

编写脚本

复制代码
vim monitor_and_restart.sh

脚本内容:

  • MONITOR_DIR: 替换为自己需要监控的路径(当前是插入U盘会在/media/guimu/路径下生成一个CENTOS文件夹, 所以根据实际情况进行修改为自己的路径即可)
  • CONTAINER_NAME: 需要重启的容器名
复制代码
#!/bin/bash

# 定义要监控的目录和容器名称
MONITOR_DIR="/media/guimu/"
CONTAINER_NAME="das-data-raw-service"

# 监控目录变化,并在变化发生时重启容器
while true; do
    inotifywait -r -e modify,create,delete,move "$MONITOR_DIR"
    echo "Detected changes in $MONITOR_DIR. Restarting container $CONTAINER_NAME..."
    docker restart "$CONTAINER_NAME"
done

添加可执行权限:

复制代码
chmod +x monitor_and_restart.sh

配置服务

复制代码
sudo vim /etc/systemd/system/monitor_and_restart.service

编辑内容:

复制代码
[Unit]
Description=Monitor and Restart das-data-raw-service Container
After=network.target

[Service]
Type=simple
ExecStart=/root/das-docker/monitor_and_restart.sh

[Install]
WantedBy=multi-user.target
  • 替换ExecStart为自己的脚本存放路径
复制代码
# 开启自启
sudo systemctl enable monitor_and_restart.service
# 开启服务
sudo systemctl start monitor_and_restart.service
# 查看服务状态
sudo systemctl status monitor_and_restart.service
# 停止服务
sudo systemctl stop monitor_and_restart.service
相关推荐
有谁看见我的剑了?6 小时前
linux 添加硬盘后系统识别不到硬盘处理
linux·运维·服务器
yc_12247 小时前
用 Visual Studio 远程调试 Linux:从零到流畅的完整指南
linux·ide·visual studio
计算机安禾7 小时前
【Linux从入门到精通】第31篇:防火墙漫谈——iptables与firewalld防护指南
linux·运维·php
下一页盛夏花开8 小时前
ubuntu 20中安装QT以后出现红色空心断点
linux·运维·ubuntu
sanshanjianke8 小时前
Thunderobot 911ME 笔记本 Linux 风扇控制研究
linux
fengyehongWorld11 小时前
TeraTerm ttl脚本登录wsl
linux·teraterm
乌托邦的逃亡者12 小时前
Linux中如何检测IP冲突
linux·运维·tcp/ip
一曦的后花园12 小时前
linux搭建promethes并对接node-exporter指标
linux·运维·服务器
乌托邦的逃亡者12 小时前
CentOS/Openeuler主机中,为一个网卡设置多个IP地址
linux·运维·网络·tcp/ip·centos
念恒1230613 小时前
进程控制---自定义Shell
linux·c语言