动态监控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
相关推荐
晚风吹人醒.3 分钟前
缓存中间件Redis安装及功能演示、企业案例
linux·数据库·redis·ubuntu·缓存·中间件
Hard but lovely31 分钟前
linux: pthread库的使用和理解
linux
这儿有一堆花2 小时前
Kali Linux:探测存活到挖掘漏洞
linux·运维·服务器
松涛和鸣3 小时前
从零开始理解 C 语言函数指针与回调机制
linux·c语言·开发语言·嵌入式硬件·排序算法
皮小白3 小时前
ubuntu开机检查磁盘失败进入应急模式如何修复
linux·运维·ubuntu
邂逅星河浪漫4 小时前
【CentOS】虚拟机网卡IP地址修改步骤
linux·运维·centos
hhwyqwqhhwy4 小时前
linux 驱动开发相关
linux·驱动开发
IT逆夜4 小时前
实现Yum本地仓库自动同步的完整方案(CentOS 7)
linux·运维·windows
S***26755 小时前
linux上redis升级
linux·运维·redis
赖small强5 小时前
【Linux 网络基础】Linux 平台 DHCP 运作原理与握手过程详解
linux·网络·dhcp