Search code examples
bashbackgroundjobs

bash script with background process


I have the following in my script:

#!/bin/bash

[ ! -S ./notify ] && find ./stylesheets/sass/ \-maxdepth 1 \-type f \-regex '.*/[^_][^/]*\.scss$' | entr \+notify &

what entr does here, is creating notify as a named pipe.

[ insert ]

while read F; do
            ...
            #some processing on found files 
            #(does not matter for this question at all)
            ...
done < notify

The problem is, first time I run the script, it sees there is no notify pipe, so it creates one, and puts the process into the background.

But then the following while loop complains it cannot find notify to read from.

However, when I run script immediately after that, so for the second time now, it continues normally the rest of the program (while loop part).

How would I fix this, so it runs all good as a whole?

EDIT:

if I put into [ insert ] placeholder above,

    sleep 1;

it works, but I would like a better solution for checking when that notify fifo exists, as sometimes it may need more than 1 sec.


Solution

  • You can always poll for the named pipe to be created:

    until [ -p notify ]; do read -t 0.1; done
    

    If you don't specifically need to maintain variables between runs, you could also consider using a script rather than entr's +notify. That would avoid the problem.