태그 보관물: inotify

inotify

여러 디렉토리 내에서 inotify-tools를 사용하여 재귀 적으로 새 파일을 지속적으로 감지 파일을 재귀 적으로 지속적으로 감지하고

방금 inotify-tools를 설치했습니다. 여러 디렉토리 내에서 notify-tools를 사용하여 새 파일을 재귀 적으로 지속적으로 감지하고 postfix를 사용하여 전자 메일을 보내려고합니다. postfix 부분을 사용하여 전자 메일 보내기를 처리 할 수 ​​있습니다. 새 파일을 탐지하려고 할 때이 문제를 해결하는 가장 좋은 방법을 찾으려고합니다. 때로는 여러 파일이 한 번에 추가되기 때문입니다.



답변

inotifywait ( inotify-tools의 일부 )는 목표를 달성하는 데 적합한 도구이며, 여러 파일이 동시에 생성되는지 여부는 중요하지 않습니다.

다음은 샘플 스크립트입니다.

#!/bin/sh
MONITORDIR="/path/to/the/dir/to/monitor/"
inotifywait -m -r -e create --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
        echo "This is the body of your mail" | mailx -s "File ${NEWFILE} has been created" "yourmail@addresshere.tld"
done

inotifywait 는이 옵션을 사용합니다.

-m는 이 스크립트가 종료됩니다 새 파일을 감지 한 후에는,이 옵션을 사용하지 않는 경우, 무기한 디렉토리를 모니터링 할 수 있습니다.

-r 은 재귀 적으로 파일을 모니터링합니다 (더 많은 dir / 파일이있는 경우 새로 작성된 파일을 발견하는 데 시간이 걸릴 수 있음)

-e create 는 모니터링 할 이벤트를 지정하는 옵션이며 새 파일을 처리하기 위해 작성 해야 합니다.

–format ‘% w % f’ 는 /complete/path/file.name 형식으로 파일을 인쇄합니다.

“$ {MONITORDIR}” 은 이전에 정의한 모니터링 할 경로가 포함 된 변수입니다.

그래서 새로운 파일이 생성되는 경우에, inotifywait를이를 감지하여 인쇄 출력한다 (/complete/path/file.name) 파이프를 행 하면서 것이다 가변이를 새에 그 출력을 지정 .

while 루프 안에는 로컬 MTA와 잘 작동 하는 mailx 유틸리티 를 사용하여 주소로 메일을 보내는 방법이 있습니다 (이 경우 Postfix).

여러 디렉토리를 모니터링하려면 inotifywait에서 허용하지 않지만 두 가지 옵션이 있습니다. 모든 디렉토리에 대해 스크립트를 작성하여 스크립트 내부에서 함수를 모니터하거나 작성하십시오.

#!/bin/sh
MONITORDIR1="/path/to/the/dir/to/monitor1/"
MONITORDIR2="/path/to/the/dir/to/monitor2/"
MONITORDIRX="/path/to/the/dir/to/monitorx/"

monitor() {
inotifywait -m -r -e create --format "%f" "$1" | while read NEWFILE
do
        echo "This is the body of your mail" | mailx -s "File ${NEWFILE} has been created" "yourmail@addresshere.tld"
done
}
monitor "$MONITORDIR1" &
monitor "$MONITORDIR2" &
monitor "$MONITORDIRX" &


답변

예를 들어, inotifywait를 사용하십시오 .

inotifywait -m /path -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        # do something with the file
    done

자세한 정보와 예제
는 inotify-tools를 사용하여 파일 시스템 이벤트에서 스크립트를 트리거하는 방법 기사를 참조하십시오 .


답변

여러 디렉토리에 대해 다음을 수행 할 수 있습니다.

#!/bin/bash


monitor() {
  inotifywait -m -r -e attrib --format "%w%f" --fromfile /etc/default/inotifywait | while read NEWFILE
  do
     echo "This is the body of your mail" | mailx -s "File ${NEWFILE} has been created" "yourmail@addresshere.tld"
  done
          }


monitor &

다음은 파일의 폴더 목록 예입니다. /etc/default/inotifywait
/etc/default/inotifywait

/home/user1
/path/to/folder2/
/some/path/


답변