Метод 1: использование inotifywait
#!/bin/bash
file="path/to/your/file.txt"
while inotifywait -e modify "$file"; do
# Execute your script here
echo "File changed!"
# Add your script execution command(s) here
done
Метод 2. Использование цикла с проверкой временных меток
#!/bin/bash
file="path/to/your/file.txt"
timestamp=$(stat -c %Y "$file")
while true; do
current_timestamp=$(stat -c %Y "$file")
if [[ "$current_timestamp" != "$timestamp" ]]; then
# Execute your script here
echo "File changed!"
# Add your script execution command(s) here
timestamp=$current_timestamp
fi
sleep 1
done
Метод 3: использование утилиты командной строки entr
#!/bin/bash
file="path/to/your/file.txt"
# Install the entr utility if not already present
# Example: sudo apt-get install entr
while true; do
# Execute your script here
echo "File changed!"
# Add your script execution command(s) here
entr -d -p -s "true" "$file"
done
Метод 4. Использование inotifywait с функцией оболочки
#!/bin/bash
file="path/to/your/file.txt"
function watch_file() {
while inotifywait -e modify "$1"; do
# Execute your script here
echo "File changed!"
# Add your script execution command(s) here
done
}
watch_file "$file"