Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- FILE_TO_WATCH="/tmp/ndnproxymain.conf"
- HASH_FILE="/var/run/ipset_watcher.hash"
- # Commands to execute when the file changes
- COMMANDS_ON_CHANGE="
- ipset flush bypass
- "
- # Function to calculate MD5 hash of the file
- calculate_md5() {
- md5sum "$FILE_TO_WATCH" | awk '{ print $1 }'
- }
- # Function to execute commands when the file changes
- on_file_change() {
- eval "$COMMANDS_ON_CHANGE"
- }
- # Function to start the script
- start() {
- # Load previous hash from file if it exists
- if [ -f "$HASH_FILE" ]; then
- PREVIOUS_HASH=$(cat "$HASH_FILE")
- else
- PREVIOUS_HASH=$(calculate_md5)
- echo "$PREVIOUS_HASH" > "$HASH_FILE"
- fi
- # Monitor the file for changes
- inotifywait -q -m -e modify,attrib,close_write "$FILE_TO_WATCH" | while read; do
- CURRENT_HASH=$(calculate_md5)
- if [ "$CURRENT_HASH" != "$PREVIOUS_HASH" ]; then
- PREVIOUS_HASH="$CURRENT_HASH"
- echo "$PREVIOUS_HASH" > "$HASH_FILE"
- on_file_change
- fi
- done &
- echo $! > /var/run/ipset_watcher.pid
- }
- # Function to stop the script
- stop() {
- if [ -f /var/run/ipset_watcher.pid ]; then
- PID=$(cat /var/run/ipset_watcher.pid)
- kill "$PID"
- sleep 1
- if ps -p "$PID" > /dev/null; then
- echo "Process $PID still running, sending SIGKILL"
- kill -9 "$PID"
- fi
- rm /var/run/ipset_watcher.pid
- else
- # Use killall as a fallback
- killall inotifywait
- fi
- }
- # Function to check the status of the script
- status() {
- if [ -f /var/run/ipset_watcher.pid ]; then
- PID=$(cat /var/run/ipset_watcher.pid)
- # Check if the PID is in the ps output
- if ps | grep -w "$PID" | grep -v grep > /dev/null; then
- echo "Monitoring process is running (PID: $PID)."
- else
- echo "Monitoring process not running, but PID file exists."
- fi
- else
- echo "Monitoring process is not running."
- fi
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- restart)
- stop
- start
- ;;
- status)
- status
- ;;
- *)
- echo "Usage: $0 {start|stop|restart|status}"
- exit 1
- ;;
- esac
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement