我有一个bash脚本,它使用inotify-tools来处理一些数据,以便知道文件系统上发生的某些事件。 如果在bash控制台中运行,它工作正常,但是当我尝试将它作为守护进程运行时,它会失败。 我认为这是因为inotifywait
命令调用的所有输出都转到一个文件,因此, | while
不再被调用。 我该如何解决这个问题? 这是我的脚本。
#!/bin/bash inotifywait -d -r \ -o /dev/null \ -e close_write \ --exclude "^[\.+]|cgi-bin|recycle_bin" \ --format "%w:%&e:%f" \ $1| while IFS=':' read directory event file do #doing my thing done
因此, -d
告诉inotifywait
以守护进程运行, -r
以recursion方式执行, -o
是保存输出的文件。 在我的情况下,该文件是/dev/null
因为我不需要输出,除了处理命令( | while...
)之外的部分
在这种情况下,您不想运行inotify-wait
作为守护程序,因为您要继续处理命令的输出。 您想用-m
替换-d
命令行选项,它告诉inotifywait
继续监视文件并继续打印到stdout
:
-m, --monitor Instead of exiting after receiving a single event, execute indefinitely. The default behaviour is to exit after the first event occurs.
如果你想要在后台运行的东西,你需要背景整个脚本。
这是一个使用nohup的解决方案:(注意在我的测试中,如果我指定了-o while循环似乎没有被评估)
nohup inotifywait -m -r \ -e close_write \ --exclude "^[\.+]|cgi-bin|recycle_bin" \ --format "%w:%&e:%f" \ $1 | while IFS=':' read directory event file do #doing my thing done >> /some/path/to/log 2>&1 &