inotify+rsync持久化的脚本引发的插曲

今天从大佬的博客里嫖到一个很赞的脚本,先附上大佬博客:https://www.cnblogs.com/f-ck-need-u/p/7220193.html

#!/bin/bash

watch_dir=/source
push_to=192.168.1.200

# First to do is initial sync
rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir $push_to:/

inotifywait -mrq -e delete,close_write,moved_to,moved_from,isdir --timefmt '%Y-%m-%d %H:%M:%S' --format '%w%f:%e:%T' $watch_dir \
--exclude=".*.swp" >>/var/log/inotifywait.log &

while true;do
     if [ -s "/var/log/inotifywait.log" ];then
        grep -i -E "delete|moved_from" /var/log/inotifywait.log >> /var/log/inotify_away.log
        rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir/ $push_to:/
        if [ $? -ne 0 ];then
           echo "$watch_dir sync to $push_to failed at `date +"%F %T"`,please check it by manual" |\
           mail -s "inotify+Rsync error has occurred" root@localhost
        fi
        cat /dev/null > /var/log/inotifywait.log
        rsync -az --delete --exclude="*.swp" --exclude="*.swx" $watch_dir/ $push_to:/
    else
        sleep 1
    fi
done

当把这个脚本用hup &挂后台时遇到了过去的残念问题——退出终端脚本后台会直接被杀掉,原因很简单。
在这里插入图片描述

如图,是SSH的子进程ssh-bash-inotify.sh
如果exit退出终端,就变成了这样
在这里插入图片描述
可见关闭终端会导致inotify.sh的父进程bash连同再上一级sshd都会被杀掉

(nohup /source/inotify.sh &)

所以用(nohup /source/inotify.sh &) 来启动就好了,() 小括号表示子shell,会直接成为主进程systemd的子进程,这样就脱离了sshd进程的控制,就可以挂后台啦
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值