Rompendo o loop while com um switch case dentro

1

Estou tendo um problema ao sair de um script bash de um loop while:

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)

O loop corretamente escuta as mudanças de arquivo no diretório dado, até aí tudo bem.

Isso também é exibido na saída do console:

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 #:

Aparentemente, parece que o script saiu bem, mas quando você o pesquisa na árvore de processos, ele ainda está lá:

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 #:

Então, minha pergunta é como realmente sair do script?

    
por FleBeling 25.11.2015 / 14:56

1 resposta

1

Eu encontrei uma solução depois de pesquisar um pouco.

O problema se origina de inotifywait subshell como @mikeserv declarado nos comentários acima.

Então eu tive que escrever um método de limpeza para isso. Meu script:

#!/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 "$@"

Nesta saída, quando um arquivo na pasta monitorada é criado e excluído:

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
    
por 27.11.2015 / 10:07