Bash script, watch folder, execute command

后端 未结 12 1321
小鲜肉
小鲜肉 2020-12-04 08:57

I am trying to create a bash script with 2 parameters:

  • a directory
  • a command.

I want to watch the

12条回答
  •  独厮守ぢ
    2020-12-04 09:15

    #!/bin/bash
    
    # Author: Devonte
    # NGINX WATCH DAEMON
    # Place file in root of nginx folder /etc/nginx
    # This will test your nginx config on any change and
    # if there are no problems it will reload your configuration
    # USAGE: sh nginx-watch.sh
    
    dir=`dirname $0`
    
    checksum_initial=`tar -cf - $dir | md5sum | awk '{print $1}'`
    checksum_now=$checksum_initial
    
    # Start nginx
    nginx
    
    while true
    do
        sleep 3
        checksum_now=`tar -cf - $dir | md5sum | awk '{print $1}'`
    
        if [ $checksum_initial != $checksum_now ]; then
            echo "[ NGINX ] A configuration file changed. Reloading..."
            nginx -t && nginx -s reload;
        fi
    
        checksum_initial=$checksum_now
    done
    

提交回复
热议问题