-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script.sh
124 lines (104 loc) · 3.6 KB
/
Script.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
#!/bin/bash
while true; do
# Ask for process name
echo "Enter the process name to monitor (e.g., starsd):"
read PROCESS_NAME
# Check if the process is running
PROCESS_COUNT=$(pgrep -xc "$PROCESS_NAME")
if [[ "$PROCESS_COUNT" -eq 0 ]]; then
echo "Error: The process '$PROCESS_NAME' is not running."
echo "What would you like to do?"
echo "1. Check the process name and enter it again"
echo "2. Continue with the entered process name"
read -p "Choose an option (1 or 2): " CHOICE
case $CHOICE in
1)
# Repeat the process name input
continue
;;
2)
# Proceed with the entered name (even though the process is not running)
echo "Proceeding with the entered process name '$PROCESS_NAME'."
break
;;
*)
# Invalid choice
echo "Invalid choice. Please try again."
continue
;;
esac
else
echo "The process '$PROCESS_NAME' is running. Proceeding with the next steps."
break
fi
done
# Ask for memory limit
echo "Enter the memory limit in kilobytes (e.g., 300000 KB for 300 MB):"
read MEMORY_LIMIT_MB
# Directory input and validation
while true; do
echo "Enter the directory to save the script (e.g., .juno, .starsd...):"
echo "Directory where your node is installed"
read SCRIPT_DIR
# Check if the specified directory exists
if [ ! -d "$SCRIPT_DIR" ]; then
echo "Error: The directory '$SCRIPT_DIR' does not exist."
echo "What would you like to do?"
echo "1. Enter the directory again"
echo "2. Exit"
read -p "Choose an option (1 or 2): " CHOICE
case $CHOICE in
1)
# Repeat the directory input
continue
;;
2)
# Exit the script
echo "Exiting the script."
exit 0
;;
*)
# Invalid choice
echo "Invalid choice. Please try again."
continue
;;
esac
else
# If the directory exists, break the loop
break
fi
done
# Form the path to save the script
SCRIPT_PATH="$SCRIPT_DIR/memory_check.sh"
# Create the main script
cat <<EOF > "$SCRIPT_PATH"
#!/bin/bash
# Process name to monitor
PROCESS_NAME="$PROCESS_NAME"
# Get the service name to restart
SERVICE_NAME=\$(cat /proc/\$(pidof "\$PROCESS_NAME")/cgroup | sed -n 's|.*/\([^.]*\)\.service|\1|p')
# Memory limit in kilobytes
MEMORY_LIMIT_MB=$MEMORY_LIMIT_MB
# Check if the process is running
PROCESS_COUNT=\$(pgrep -c "\$PROCESS_NAME")
if [[ "\$PROCESS_COUNT" -eq 0 ]]; then
echo -e "\$(date): Process \$PROCESS_NAME is not running. Skipping memory check.\n" >> /var/log/memory_check.log
exit 0
fi
# Get the memory usage of the process (in KB)
MEMORY_USAGE=\$(ps --no-headers -o rss -p \$(pgrep "\$PROCESS_NAME") | awk '{sum+=$1} END {print sum}')
# Check if the limit is exceeded
if [[ "\$MEMORY_USAGE" -gt "\$MEMORY_LIMIT_MB" ]]; then
echo -e "\$(date): Memory limit \$MEMORY_USAGE KB exceeded for \$PROCESS_NAME. Restarting service...\n" >> /var/log/memory_check.log
# Restart the service
if systemctl restart "\$SERVICE_NAME"; then
echo -e "\$(date): Service \$SERVICE_NAME restarted successfully.\n" >> /var/log/memory_check.log
else
echo -e "\$(date): Failed to restart \$SERVICE_NAME.\n" >> /var/log/memory_check.log
fi
fi
EOF
# Make the new script executable
chmod +x "$SCRIPT_PATH"
# Information about the created script
echo "Script created and saved in $SCRIPT_PATH"