0

I have a running bash script that is forking. It looks like this:

#!/bin/bash
/usr/bin/killall inotifywait

/usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder1/ | while IFS= read -r FILE; do /bin/chmod 770 "$FILE"; done >/dev/null 2>&1 &
/usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder2/ | while IFS= read -r FILE; do /bin/chmod 770 "$FILE"; done >/dev/null 2>&1 &
/usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder3/ | while IFS= read -r FILE; do /bin/chmod 770 "$FILE"; done >/dev/null 2>&1 &
/usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder4/ | while IFS= read -r FILE; do /bin/chmod 770 "$FILE"; done >/dev/null 2>&1 &

If i run the script it creates 8 processes:

  • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder1/...
  • /bin/bash ./scriptname.sh
  • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder2/...
  • /bin/bash ./scriptname.sh
  • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder3/...
  • /bin/bash ./scriptname.sh
  • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder4/...
  • /bin/bash ./scriptname.sh

Why does it not forking like this?

  • /bin/bash ./scriptname.sh
    • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder1/...
    • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder2/...
    • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder3/...
    • /usr/bin/inotifywait -mrq -e CREATE --format %w%f /home/sftp/folder4/...

Thank you for your answer, starflighter

1

You are piping output of inotifywait into a while loop. That while loop in this context will run in a subshell, which is what these extra bash processes are.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.