blob: af8fd9b3c6e7a5861d6c109afc886af7f0aa30a0 (
plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#!/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"
OUTPUT_ROOT_DIR="/var/www/html/soulmining"
COOLDOWN_TIME=60
LAST_RUN_FILE="/tmp/last_run.time"
LOG_FILE="/var/log/lume-watcher.log"
PID_FILE="/var/run/lume-watcher.pid"
MAX_LOG_SIZE="1M" # 設定可能なログサイズ (例: 500K, 1M, 1G)
# 現在の時間を取得
current_time() {
date +%s
}
# 最後にコマンドを実行した時間を取得する(存在しない場合は0を返す)
get_last_run_time() {
if [ -f "$LAST_RUN_FILE" ]; then
cat "$LAST_RUN_FILE"
else
echo 0
exit 99
fi
}
# 最後にコマンドを実行した時間を記録する
set_last_run_time() {
current_time > "$LAST_RUN_FILE"
}
# ログファイルのサイズをチェックし、削除
check_and_rotate_log() {
if [ -f "$LOG_FILE" ]; then
# 現在のログサイズを取得
current_log_size=$(du -b "$LOG_FILE" | cut -f1)
# MAX_LOG_SIZEをバイト単位に変換
max_size_value=${MAX_LOG_SIZE%[KMG]}
max_size_unit=${MAX_LOG_SIZE: -1}
case $max_size_unit in
K) max_size_in_bytes=$((max_size_value * 1024));;
M) max_size_in_bytes=$((max_size_value * 1024 * 1024));;
G) max_size_in_bytes=$((max_size_value * 1024 * 1024 * 1024));;
*) max_size_in_bytes=$max_size_value;;
esac
# ログファイルのサイズがMAX_LOG_SIZEを超えているかチェック
if [ "$current_log_size" -ge "$max_size_in_bytes" ]; then
rm "$LOG_FILE"
touch "$LOG_FILE"
fi
fi
}
# 監視開始を行う
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
check_and_rotate_log
echo "$(date): Executing command" >> "$LOG_FILE"
sleep 20
cd $OUTPUT_ROOT_DIR || 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() {
check_and_rotate_log
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
|