inotifywait shell 脚本作为守护进程运行

Nic*_*las 6 linux shell sh inotify inotifywait

我有一个脚本来监视目录(递归)并在文件更改时执行命令。当监控标志如下使用时,这是正常工作的:

#!/bin/sh

inotifywait -m -r /path/to/directory |
    while read path action file; do
            if [ <perform a check> ]
            then
                my_command
            fi
    done
Run Code Online (Sandbox Code Playgroud)

但是,我想在启动时和后台运行它,所以天真地认为我可以将 -m 标志更改为 -d(作为守护进程运行 inotifywait,并包含一个 --outfile 位置),然后将其添加到 rc.local在启动时运行这个。我哪里错了?

Gon*_*heu 5

Incron是一个类似于 cron 的 inotify 事件守护进程。

只需使用incrontab和一个任务条目:

/path/to/directory IN_ALL_EVENTS /usr/local/bin/my-script $@ $# $%
Run Code Online (Sandbox Code Playgroud)

/local/bin/my-script将是:

#! /bin/bash
local path=$1
local action=$2
local file=$3
if [ <perform a check> ]
then
  my_command 
fi
Run Code Online (Sandbox Code Playgroud)


Jeb*_*bby 3

&您需要在命令末尾添加一个/etc/rc.local

&在命令末尾添加一个表示在后台运行该程序,以便用户仍然可以进行输入。