【问题标题】:run inotifywait on background在后台运行 inotifywait
【发布时间】:2013-09-19 17:47:29
【问题描述】:

我从 linuxaria.com 复制了这段代码作为示例,并且在我的情况下工作得很好,问题是当我从终端 inotifywait stop 退出时。即使在退出终端后,我也想在后台运行。我该怎么做?

#!/bin/sh

# CONFIGURATION
DIR="/tmp"
EVENTS="create"
FIFO="/tmp/inotify2.fifo"


on_event() {
  local date=$1
  local time=$2
  local file=$3

  sleep 5

  echo "$date $time Fichier créé: $file"
}

# MAIN
if [ ! -e "$FIFO" ]
then
  mkfifo "$FIFO"
fi

inotifywait -m -e "$EVENTS" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' "$DIR" >       "$FIFO" &
INOTIFY_PID=$!


while read date time file
do
  on_event $date $time $file &
done < "$FIFO"

【问题讨论】:

    标签: bash inotify inotifywait


    【解决方案1】:

    我用它做了一个“服务”。所以我可以像普通服务一样停止/启动它,它也会在重启后启动:

    这是在 Centos 发行版上制作的,所以如果它立即适用于其他人,我不会。

    在服务目录下创建一个有执行权限的文件

    /etc/init.d/服务名

    #!/bin/bash
    
    # chkconfig: 2345 90 60
    
    case "$1" in
    start)
       nohup SCRIPT.SH > /dev/null 2>&1 &
       echo $!>/var/run/SCRIPT.SH.pid
       ;;
    stop)
       pkill -P `cat /var/run/SCRIPT.SH.pid`
       rm /var/run/SCRIPT.SH.pid
       ;;
    restart)
       $0 stop
       $0 start
       ;;
    status)
       if [ -e /var/run/SCRIPT.SH.pid ]; then
          echo SCRIPT.SH is running, pid=`cat /var/run/SCRIPT.SH.pid`
       else
          echo SCRIPT.SH is not running
          exit 1
       fi
       ;;
    *)
       echo "Usage: $0 {start|stop|status|restart}"
    esac
    
    exit 0
    

    大写的所有内容都应更改为脚本名称。

    # chkconfig: 2345 90 60 行可以在系统重新启动时启动服务。这可能不像发行版那样在 ubuntu 中工作。

    【讨论】:

      【解决方案2】:

      您可以使用screennohup 运行脚本,但我不确定这会有什么帮助,因为脚本似乎没有将其输出记录到任何文件中。

      nohup bash script.sh </dev/null >/dev/null 2>&1 &
      

      或者

      screen -dm bash script.sh </dev/null >/dev/null 2>&1 &
      

      否认也适用:

      bash script.sh </dev/null >/dev/null 2>&1 & disown
      

      你应该只测试哪一个在终端退出时不允许命令挂起或挂断。

      如果您想将输出记录到文件中,可以尝试以下版本:

      nohup bash script.sh </dev/null >/path/to/logfile 2>&1 &
      screen -dm bash script.sh </dev/null >/path/to/logfile 2>&1 &
      bash script.sh </dev/null >/path/to/logfile 2>&1 & disown
      

      【讨论】:

        【解决方案3】:

        将 -m 替换为

        -d -o /dev/null

        即:

        inotifywait -d -o /dev/null -e "$EVENTS" --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' >" $DIR" > "$FIFO" & INOTIFY_PID=$!

        您可以在以下位置查看 inotifywait 帮助手册:

        https://helpmanual.io/help/inotifywait/

        【讨论】:

          【解决方案4】:

          我发现最好的方法是创建一个 systemd 服务。

          /lib/systemd/system/checkfile.service中创建systemd文件:

          sudo vim /lib/systemd/system/checkfile.service
          

          然后粘贴到那里:

          [Unit]
          Description = Run inotifywait in backgoround
          
          [Service]
          User=ubuntu
          Group=ubuntu
          ExecStart=/bin/bash /path_to/script.sh
          RestartSec=10
          
          [Install]
          WantedBy=multi-user.target
          

          /path_to/script.sh,你可以有这个:

          inotifywait -m /path-to-dir -e create -e moved_to |
              while read dir action file; do
                  echo "The file '$file' appeared in directory '$dir' via '$action'" >> /dir/event.txt
              done
          

          确保您的文件可由用户执行:

          sudo chmod +x /path_to/script.sh
          

          创建两个文件后,重新加载 systemd 管理器配置:

          sudo systemctl daemon-reload
          

          现在您可以对脚本使用启动/停止/启用:

          sudo systemctl enable checkfile
          sudo systemctl start checkfile
          

          确保在执行前替换文件/目录/用户/组值。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-04-06
            • 2016-05-02
            • 2011-08-19
            • 2011-12-28
            • 2015-10-18
            • 2017-01-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多