summaryrefslogtreecommitdiff
path: root/lume-watcher
diff options
context:
space:
mode:
authorhaturatu <taro@eyes4you.org>2024-08-20 22:46:54 +0900
committerhaturatu <taro@eyes4you.org>2024-08-20 22:46:54 +0900
commit26d9051ab7ad4ec2202e0e68b6dd98df3b70bed6 (patch)
tree883fc4337025a802a24a1386d1b7d654e22e68db /lume-watcher
first commit
Diffstat (limited to 'lume-watcher')
-rw-r--r--lume-watcher112
1 files changed, 112 insertions, 0 deletions
diff --git a/lume-watcher b/lume-watcher
new file mode 100644
index 0000000..3389292
--- /dev/null
+++ b/lume-watcher
@@ -0,0 +1,112 @@
+#!/bin/bash
+### BEGIN INIT INFO
+# Provides: lume-watcher
+# Required-Start: $remote_fs $syslog
+# Required-Stop: $remote_fs $syslog
+# Default-Start: 2 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: Watches directory and triggers Lume task
+# Description: Watches the specified directory and triggers the Deno Lume task when changes are detected.
+### END INIT INFO
+
+WATCHED_DIR="/var/www/html/soulmining/src/"
+COMMAND="/home/haturatu/.deno/bin/deno task lume --dest=site"
+FLAG_FILE="/tmp/command_running.flag"
+COOLDOWN_TIME=60 # クールダウン時間(秒)
+LAST_RUN_FILE="/tmp/last_run.time"
+PIDFILE="/var/run/lume-watcher.pid"
+
+# 現在の時間を取得
+current_time() {
+ date +%s
+}
+
+# 最後にコマンドを実行した時間を取得する(存在しない場合は0を返す)
+get_last_run_time() {
+ if [ -f "$LAST_RUN_FILE" ]; then
+ cat "$LAST_RUN_FILE"
+ else
+ echo 0
+ fi
+}
+
+# 最後にコマンドを実行した時間を記録する
+set_last_run_time() {
+ current_time > "$LAST_RUN_FILE"
+}
+
+# デーモンの開始
+start() {
+ echo "Starting lume-watcher..."
+ if [ -f "$PIDFILE" ]; then
+ echo "lume-watcher is already running."
+ exit 1
+ fi
+
+ # 実際の監視プロセスをバックグラウンドで実行
+ (
+ while true; do
+ if [ ! -f "$FLAG_FILE" ]; then
+ inotifywait -e modify,create,delete -r "$WATCHED_DIR"
+ last_run_time=$(get_last_run_time)
+ now=$(current_time)
+ elapsed_time=$((now - last_run_time))
+
+ if [ "$elapsed_time" -ge "$COOLDOWN_TIME" ]; then
+ echo "Change detected, executing command..."
+ touch "$FLAG_FILE"
+ $COMMAND
+ rm -f "$FLAG_FILE"
+ set_last_run_time
+ fi
+ fi
+ sleep 1
+ done
+ ) &
+ echo $! > "$PIDFILE"
+ echo "lume-watcher started."
+}
+
+# デーモンの停止
+stop() {
+ echo "Stopping lume-watcher..."
+ if [ ! -f "$PIDFILE" ]; then
+ echo "lume-watcher is not running."
+ exit 1
+ fi
+
+ kill "$(cat $PIDFILE)" && rm -f "$PIDFILE"
+ echo "lume-watcher stopped."
+}
+
+# デーモンのステータス確認
+status() {
+ if [ -f "$PIDFILE" ]; then
+ echo "lume-watcher is running, PID: $(cat $PIDFILE)"
+ else
+ echo "lume-watcher 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
+