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