我有一个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以递归方式运行,-o是保存输出的文件。在我的例子中,文件是/dev/null,因为除了处理命令后面的部分(| while...)之外,我并不真正需要输出。
发布于 2012-05-10 19:53:54
在这种情况下,您不希望将inotify-wait作为守护进程运行,因为您希望继续处理命令的输出。您希望将-d命令行选项替换为-m,这将告诉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.如果你想让程序在后台运行,你需要在后台运行整个脚本。
发布于 2012-12-13 06:23:42
这里有一个使用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 &https://stackoverflow.com/questions/10533200
复制相似问题