-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.sh
33 lines (28 loc) · 1.18 KB
/
monitor.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
# Script to monitor templates for json file changes and rebuild the templates.json file when detected (inotify/Linux)
# Run it once to set up monitoring. For windows, run monitor.ps1 in PowerShell (see README.md)
# Install inotify-tools if not already installed
if ! command -v inotifywait &> /dev/null; then
if command -v yum &> /dev/null; then
sudo yum install -y inotify-tools
elif command -v apk &> /dev/null; then
sudo apk add inotify-tools
elif command -v apt &> /dev/null; then
sudo apt-get update
sudo apt-get install -y inotify-tools
else
echo "Unsupported package manager."
exit 1
fi
fi
# Lock the script using fcntl to prevent duplicate runs:
scriptname=$(basename "$0")
pidfile="/var/run/${scriptname}.pid"
exec 200>"$pidfile"
flock -n 200 || { echo "Another instance is already running."; exit 1; }
while true; do #shellcheck disable=SC2034 # FILE appears unused but flags collation
inotifywait -r -e modify,create,delete --format '%w%f' ./templates/*.json | while read -r FILE; do
./collate.sh # Run collate script on every .json file change
done
done
flock -u 200 # Unlock the script on exit