다른 프로세스에 출력이 없으면 bash 명령을 반복하십시오.

다른 프로세스에 출력이 없으면 bash 명령을 반복하십시오.

나는 뛰고있어fs_usage내 파일 시스템의 개체에 대한 액세스를 감지합니다.

sudo fs_usage -w | grep -E 'object'

touch이제 5초 이내에 위 명령의 새 출력이 없는 한 이 개체에 대해 5초마다 명령을 실행하고 싶습니다.

답변1

sudo fs_usage -w | while true; do
    if read -rt5 && [[ $REPLY =~ objectpattern ]]; then
        # Some output happened in the last 5 seconds that matches object pattern
        :
    else 
        touch objectfile
    fi
done

물론 사용한다는 것은 read -t일부 출력이 가능하다는 것을 의미합니다.아니요match objectpattern; 이런 일이 발생하면 파일이 터치됩니다. 이것을 피하고 싶다면 좀 더 정교해져야 합니다.

timeout=5
sudo fs_usage -w | while true; do
    (( mark = SECONDS + timeout ))
    if !read -rt$timeout; then
        touch objectfile
        timeout=5
    elif ![[ $REPLY =~ objectpattern ]]; then
        # Some output happened within timeout seconds that does _not_ match.
        # Reduce timeout by the elapsed time.
        (( timeout = mark - SECONDS ))
        if (( timeout < 1 )); then
            touch objectfile
            timeout=5
        fi
    else
        timeout=5
    fi
done

답변2

내가 올바르게 이해했다면 아마도 다음을 수행하고 싶을 것입니다.

sh -c '{ fsusage             #your command runs (indefintely?)
         kill -PIPE "$$"     #but when it completes, so does this shell
       } >&3 &               #backgrounded and all stdout writes to pipe
       while sleep 5         #meanwhile, every 5 seconds a loop prints
       do    echo            #a blank line w/ echo
       done' 3>&1 |          #also to a pipe, read by an unbuffered (GNU) sed
sed -u '
### if first input line, insert shell init to stdout
### for seds [aic] commands continue newlines w/ \escapes
### and otherwise \escape only all other backslashes
1i\
convenience_func(){ : this is a function  \\\
                      declared in target  \\\
                      shell and can be    \\\
                      called from sed.; }
### if line matches object change it to command
/object/c\
# this is an actual command sent to a shell for each match
### this is just a comment - note the \escaped newlines above                    
### delete all other nonblank lines; change all blanks
/./d;c\
# this is a command sent to a shell every ~5 seconds
' | sh -s -- This is the target shell and these are its   \
             positional parameters. These can be referred \
             to in sed\'s output like '"$1"' or '"$@"' as \
             an array. They can even be passed along to   \
             'convenience_func()' as arguments.

위의 내용 중 약 90%가 댓글입니다. 기본적으로 결론은...

sh -c '(fsusage;kill "$$") >&3 &
       while sleep 5; do echo; done
' 3>&1| 
sed -nue '/pattern/c\' -e 'echo match
          /./!c\'      -e 'touch -- "$1"
' | sh -s -- filename

관련 정보