-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.sh
executable file
·158 lines (127 loc) · 3.92 KB
/
runner.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
start=$(date)
SECONDS=0
echo "Script started at: $start"
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <folder_prefix> <script_prefix>"
exit 1
fi
folder_prefix="$1"
script_prefix="$2"
export PYTHONUNBUFFERED="1"
# Create logs directory if it doesn't exist
mkdir -p logs
# Start temperature monitor in the background
(
while true; do
if ! command -v sensors >/dev/null 2>&1; then
echo "sensors command not found. Please install lm-sensors."
exit 1
fi
over_temp_cores=0
while read -r line; do
if [[ $line =~ ^Core\ [0-9]+: ]]; then
temp=$(echo "$line" | awk '{print $3}' | tr -d '+°C')
temp=${temp%.*}
if (( temp > 82 )); then
((over_temp_cores++))
fi
fi
done < <(sensors)
if (( over_temp_cores > 1 )); then
echo "$(date '+%Y-%m-%d %H:%M:%S') - Cores Over Temp: $over_temp_cores"
fi
sleep 900
done
) 2>&1 &
sensors_pid=$!
echo "Temp monitor PID: $sensors_pid"
# Start memory monitor in the background
(
while true; do
if ! command -v free >/dev/null 2>&1; then
echo "free command not found."
exit 1
fi
mem_info=$(free -m | awk '/^Mem:/ {print $2, $3}')
total_mem=$(echo $mem_info | awk '{print $1}')
used_mem=$(echo $mem_info | awk '{print $2}')
if [ -z "$total_mem" ] || [ -z "$used_mem" ]; then
echo "Could not retrieve memory information."
sleep 900
continue
fi
percent=$(awk -v total="$total_mem" -v used="$used_mem" 'BEGIN{printf "%.2f", (used/total)*100}')
if (( $(echo "$percent > 90" | bc -l) )); then
echo "$(date '+%Y-%m-%d %H:%M:%S') - RAM Over 90%: $percent%"
fi
sleep 900
done
) 2>&1 &
mem_pid=$!
echo "Mem monitor PID: $mem_pid"
find src -type d -name "${folder_prefix}*" | while read -r folder; do
find "$folder" -type f \( -name "${script_prefix}*.R" -o -name "${script_prefix}*.py" \) -not -path "*/archive/*" | sort | while read -r file; do
echo -n "Running $file "
directory=$(dirname "$file")
filename=$(basename "$file")
log_file="logs/${directory//\//_}_$filename.log"
rm "$log_file" 2> /dev/null
case $file in
*.py)
interpreter="python"
;;
*.R)
interpreter="Rscript"
;;
*)
continue
;;
esac
# Run the interpreter directly and capture its PID
cd "$directory"
"$interpreter" "$filename" > "../../$log_file" 2>&1 &
interpreter_pid=$!
cd - >/dev/null
echo "PID: $interpreter_pid"
# Wait for the interpreter process to finish
wait $interpreter_pid
wait_exit_status=$?
if [ $wait_exit_status -eq 0 ]; then
echo "Finished"
else
echo "Error. Aborting."
# Kill the monitors before exiting
if kill -0 $sensors_pid 2>/dev/null; then
echo "Killing temp monitor."
kill $sensors_pid
else
echo "Temp monitor not running."
fi
if kill -0 $mem_pid 2>/dev/null; then
echo "Killing mem monitor."
kill $mem_pid
else
echo "Mem monitor not running."
fi
exit 1
fi
done
done
# Kill the monitors at the end
if kill -0 $sensors_pid 2>/dev/null; then
echo "Killing temp monitor."
kill $sensors_pid
else
echo "Temp monitor not running."
fi
if kill -0 $mem_pid 2>/dev/null; then
echo "Killing mem monitor."
kill $mem_pid
else
echo "Mem monitor not running."
fi
end=$(date)
duration=$SECONDS
echo "Script ended at: $end"
echo "Total duration: $duration seconds."