while 루프 내에서 bash 스크립트를 종료하는 데 문제가 있습니다.
while read -r dir event name; do
case $event in
OPEN)
chown $VHOST:$VHOST $WEBPATH/$name;
echo "The file \"$name\" was created (not necessarily writable)";
;;
WRITE)
echo "The file \"$name\" was written to";
;;
DELETE)
echo "The file \"$name\" was deleted";
exit 0;
;;
esac
done < <(/usr/bin/inotifywait -m $WEBPATH)
루프는 주어진 디렉터리에서 파일 변경 사항을 올바르게 수신하므로 지금까지는 괜찮습니다.
이는 콘솔 출력에도 표시됩니다.
root #: bash /var/scriptusr/letsencrypt/dir-change
Setting up watches.
Watches established.
The file "tes" was created (not necessarily writable)
The file "tes" was deleted
root #:
분명히 스크립트는 정상적으로 종료되는 것 같지만 프로세스 트리에서 검색하면 여전히 남아 있습니다.
root #: ps aux | grep dir-
root 5549 0.0 0.0 14700 1716 pts/0 S 14:46 0:00 bash /var/scriptusr/letsencrypt/dir-change
root 5558 0.0 0.0 14184 2184 pts/1 S+ 14:46 0:00 grep dir-
root #:
내 질문은 실제로 스크립트를 종료하는 방법입니다.
답변1
몇 가지 검색 끝에 해결책을 찾았습니다.
문제는 inotifywait
위의 설명에서 @mikeserv가 언급한 하위 쉘에서 비롯됩니다.
그래서 정리 방법을 작성해야합니다. 내 스크립트:
#!/bin/bash
#
#
# script for immediatly changing the owner and group of the let's encrypt challenge file in the given webroot
Pidfile="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/run-file-chowner.pid
echo $$ > $Pidfile
function terminate_process () {
trap - SIGHUP SIGINT SIGTERM SIGQUIT
printf "\nTerminating process...\n"
rm "$Pidfile" > /dev/null 2>&1;
kill -- -$$
exit $1
}
function main () {
trap terminate_process SIGHUP SIGINT SIGTERM SIGQUIT
local OPTIND D opt
while getopts D: opt;
do
case $opt in
D)
Domain=$OPTARG;;
esac
done
shift $((OPTIND-1))
case $Domain in
'domain-b.com')
VHost="doma-www"
;;
'domain-a.com')
VHost="domb-www"
;;
*)
printf "\nScript usage : [ $0 -D \"example.com\" ]\n\n"
exit 1;
;;
esac
WebPath=/var/www/$Domain/$VHost/htdocs/public/.well-known/acme-challenge
inotifywait -m $WebPath | while read -r dir event name; do
case $event in
CREATE)
chown $VHost:$VHost $WebPath/$name
printf "\nOwner and group of \"$name\" were changed to $VHost...\n"
;;
DELETE)
printf "\nThe file \"$name\" was deleted\n"
terminate_process 0
;;
*)
printf "\nEvent $event was triggered.\n"
;;
esac
done
}
main "$@"
감시 폴더에서 파일을 생성하고 삭제할 때의 출력은 다음과 같습니다.
root #: bash file-chowner -D dom-a.com
Setting up watches.
Watches established.
Owner and group of "test" were changed to doma-www...
Event OPEN was triggered.
Event ATTRIB was triggered.
Event CLOSE_WRITE,CLOSE was triggered.
Event ATTRIB was triggered.
The file "test" was deleted
Terminating process...
Terminated
Terminating process...
Terminated