bash - 在后台运行 inotifywait

标签 bash inotify inotifywait

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

#!/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"

最佳答案

您可以使用 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

关于bash - 在后台运行 inotifywait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18901622/

相关文章:

python - 如果文件在两台机器中都丢失,如何以非零状态退出 shell 脚本?

linux - bash:检查目录中的多个文件是否包含列表中的字符串

string - Bash:从变量 B 中删除变量 A 中的一系列字符串?

linux - 有什么办法可以列出 Linux 内核支持的 inotify 事件吗?

git - inotify:除了 .gitignore 中的文件外,还可以观察文件变化吗?

linux - 在 linux 上使用 expect 和 inotifywait 观察文件夹中的变化

linux - 为什么 "expr $n - 1"有效,而 "expr $n -1"无效?

bash - 通过 rsync 发送文件时,inotify 识别出错误的文件名

linux - 如何检测没有人在 Linux 中写入文件?

linux - 根据文件名上的 grep 获取到最新文件的自动更新符号链接(symbolic link)