1

我在 CentOS 7 上使用inotify-tools ( inotifywait) 在每个文件创建时执行一个 php 脚本。

当我运行以下脚本时:

#!/bin/sh
MONITORDIR="/path/to/some/dir"
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
    php /path/to/myscript.php ${NEWFILE}
done

我可以看到有2个过程:

# ps -x | grep mybash.sh
    27723 pts/4    S+     0:00 /bin/sh /path/to/mybash.sh
    27725 pts/4    S+     0:00 /bin/sh /path/to/mybash.sh
    28031 pts/3    S+     0:00 grep --color=auto mybash.sh

为什么会这样,我该如何解决?

4

1 回答 1

0

一个管道分成多个进程。因此,您拥有父脚本,以及运行while read循环的单独子 shell。

如果您不想这样,请改用 bash 或 ksh 中可用的进程替换语法(请注意,下面的 shebang 不再是#!/bin/sh):

#!/bin/bash
monitordir=/path/to/some/dir

while read -r newfile; do
    php /path/to/myscript.php "$newfile"
done < <(inotifywait -m -r -e create --format '%w%f' "$monitordir")
于 2017-05-03T01:21:50.127 回答