Is the inotify script executed twice?

I use inotify-tools ( inotifywait) on CentOS 7 to run a PHP script for each file creation.

When I run the following script:

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

I see that there are 2 processes:

# 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

Why is this and how can I fix it?

0
source share
1 answer

The pipeline breaks down into several processes. Thus, you have a parent script, as well as a separate subshell that executes the loop while read.

If you don't want this, use the process substitution syntax available in bash or ksh instead (note that shebang is no longer below #!/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")
0
source

Source: https://habr.com/ru/post/1676161/


All Articles