#!/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" COOLDOWN_TIME=60 LAST_RUN_FILE="/tmp/last_run.time" LOG_FILE="/var/log/lume-watcher.log" PID_FILE="/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 } # 最後にコマンドを実行した時間を記録する 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" } # デーモンの開始 monitor_directory() { inotifywait -m -r -e modify,create,delete "$WATCHED_DIR" | while read -r directory events filename; do echo "$(date): Change detected" >> "$LOG_FILE" last_run_time=$(get_last_run_time) now=$(current_time) if [ $((now - last_run_time)) -ge $COOLDOWN_TIME ]; then echo "$(date): Executing command" >> "$LOG_FILE" sleep 10 cd /var/www/html/soulmining || exit $COMMAND >> "$LOG_FILE" 2>&1 cd ~ || exit set_last_run_time else echo "$(date): Command not executed due to cooldown" >> "$LOG_FILE" fi done } start() { echo "Starting lume-watcher..." monitor_directory & echo $! > "$PID_FILE" echo "Service started." } stop() { echo "Stopping lume-watcher..." if [ -f "$PID_FILE" ]; then kill $(cat "$PID_FILE") rm -f "$PID_FILE" echo "Service stopped." else echo "Service not running." fi } status() { if [ -f "$PID_FILE" ]; then cat "$PID_FILE" echo "Service is running." else echo "Service is stopped." fi } case "$1" in start) start ;; stop) stop ;; status) status ;; restart) stop start ;; *) echo "Usage: lume-watcher {start|stop|status|restart}" exit 1 ;; esac exit 0