Advertisement
Guest User

Untitled

a guest
Aug 10th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.25 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. FILE_TO_WATCH="/tmp/ndnproxymain.conf"
  4. PREVIOUS_HASH=""
  5.  
  6. # Commands to execute when the file changes
  7. COMMANDS_ON_CHANGE="
  8.    ipset flush bypass
  9.    /opt/etc/init.d/S52ipset-dns restart
  10. "
  11.  
  12. # Function to calculate MD5 hash of the file
  13. calculate_md5() {
  14.     md5sum "$FILE_TO_WATCH" | awk '{ print $1 }'
  15. }
  16.  
  17. # Function to execute commands when the file changes
  18. on_file_change() {
  19.     eval "$COMMANDS_ON_CHANGE"
  20. }
  21.  
  22. # Function to start the script
  23. start() {
  24.     echo "Starting ipset watcher..."
  25.     PREVIOUS_HASH=$(calculate_md5)
  26.     inotifywait -q -m -e close_write "$FILE_TO_WATCH" | while read _; do
  27.         CURRENT_HASH=$(calculate_md5)
  28.         if [ "$CURRENT_HASH" != "$PREVIOUS_HASH" ]; then
  29.             PREVIOUS_HASH="$CURRENT_HASH"
  30.             on_file_change
  31.         fi
  32.     done &
  33.     echo $! > /var/run/ipset_watcher.pid
  34. }
  35.  
  36. # Function to stop the script
  37. stop() {
  38.     echo "Stopping ipset watcher..."
  39.     kill $(cat /var/run/ipset_watcher.pid)
  40.     rm /var/run/ipset_watcher.pid
  41. }
  42.  
  43. case "$1" in
  44.     start)
  45.         start
  46.         ;;
  47.     stop)
  48.         stop
  49.         ;;
  50.     restart)
  51.         stop
  52.         start
  53.         ;;
  54.     *)
  55.         echo "Usage: $0 {start|stop|restart}"
  56.         exit 1
  57.         ;;
  58. esac
  59.  
  60. exit 0
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement