Inotify

文件监控-inotify安装与使用分享

Linux下使用inotify-tools实现文件监控并自动推送钉钉webhook告警

无密码登录SSH
ssh-keygen -t rsa -f ~/.ssh/id_rsa_bin
ssh-copy-id -i ~/.ssh/id_rsa_bin.pub me@8.8.8.8
ssh -i ~/.ssh/id_rsa_bin me@8.8.8.8

#vim /usr/local/bin/yt_rsync
#!/bin/bash

/usr/bin/inotifywait -m -r --format "%e %w%f" -e moved_to -e close_write --exclude "swp|swx|~$" /home/me/AV/ | while read event file; do
    #ext=${file##*.}
    ext=${file: -4}
    if [[ $ext == .mp3 || $ext == .mp4 || $ext == webm || $ext == .vtt ]]; then
        /usr/bin/rsync -avzh -e "ssh -i /root/.ssh/id_rsa_bin" /home/me/AV/*.mp* /home/me/AV/*.webm /home/me/AV/*.vtt me@8.8.8.8:~/AV/
    fi
done

#chmod +x yt-rsync

编写systemctl服务文件:
#vim /etc/systemd/system/ytdlp-monitor.service 
[Unit]
Description=yt File Monitor Service for Directory Changes Notification via DingTalk

[Service]
Type=simple
ExecStart=/usr/local/bin/yt-rsync

# Restart service on failure to ensure it keeps running.
Restart=on-failure 
RestartSec=5s 

# Environment variables can be set here if needed.
# Example:
# Environment="VAR_NAME=value"

[Install]
WantedBy=multi-user.target

启动服务:
systemctl daemon-reload
systemctl enable --now ytdlp-monitor.service 
systemctl status ytdlp-monitor.service 


接收主机webm转mp4, vtt转srt
#vim ~/bin/yt-trans
/usr/bin/inotifywait -m -r --format "%e %w%f" -e moved_to -e close_write --exclude "swp|swx|~$" /home/mars/AV/ | while read event file; do
    ext=${file: -4}
    if [[ $ext == webm ]]; then
        name=`echo $file | cut -d'.' -f1`;
        /usr/local/bin/ffmpeg -i "$file" -c copy "$name.mp4"
        rm -f "%file"
    fi
    if [[ $ext == .vtt ]]; then
        name=`echo $file | cut -d'.' -f1`;
        /usr/local/bin/ffmpeg -i "$file" "$name.srt"
        rm -f "%file"
    fi
done

systemctl start ytdlp-trans