-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_shell.sh
62 lines (52 loc) · 1.78 KB
/
mem_shell.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
#!/bin/bash
# Ensure no other instance of the Python script is running
if pgrep -f "/root/anaconda3/envs/DL/bin/python3.8 main.py --metric mem" > /dev/null; then
echo "Another instance of the process is already running. Exiting."
exit 1
fi
# Get current readable timestamp
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
# Set Zilean-Forge directories
forge_dir="/root/PycharmProjects/Tiny-Zilean-Forge"
logs_dir="$forge_dir/logs"
config_dir="$forge_dir/config"
storage_dir="$forge_dir/storage"
# Create directories if they don't exist
mkdir_if_not_exists() {
if [ ! -d "$1" ]; then
mkdir -p "$1" || { echo "Failed to create directory: $1"; exit 1; }
fi
}
# Create necessary directories
mkdir_if_not_exists "$logs_dir"
mkdir_if_not_exists "$config_dir"
mkdir_if_not_exists "$storage_dir"
# Change to the appropriate directory
cd "$forge_dir" || { echo "Failed to change directory: $forge_dir"; exit 1; }
# Define cleanup actions when the script exits
cleanup() {
pkill stress-ng # Kill stress-ng process
}
# Trap EXIT signal to execute cleanup function
trap cleanup EXIT
# Define function to start Python script
start_python_script() {
while true; do
# Start Python script in background
/root/anaconda3/envs/DL/bin/python3.8 main.py --metric mem &
python_pid=$! # Get Python process PID
wait $python_pid # Wait for Python process to finish
python_exit_status=$? # Save Python process exit status
if [ $python_exit_status -eq 0 ]; then
break
else
echo "Python script exited with an error. Restarting in 5 minutes..."
# Cleanup residual processes
pkill -f "/root/anaconda3/envs/DL/bin/python3.8 main.py --metric mem"
pkill stress-ng
sleep 300 # Wait 5 minutes before restarting
fi
done
}
# Start Python script
start_python_script