forked from frappe/frappe_docker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis_memory_settings.sh
executable file
·82 lines (70 loc) · 2.01 KB
/
redis_memory_settings.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
#!/bin/bash
# Function to show usage
show_usage() {
echo "Usage: $0 [enable|disable|status]"
echo " enable - Enable Redis memory overcommit"
echo " disable - Disable Redis memory overcommit"
echo " status - Show current memory overcommit status"
exit 1
}
# Function to check if running as root
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Please run as root or with sudo"
exit 1
fi
}
# Function to get current status
get_status() {
local current_value
current_value=$(sysctl vm.overcommit_memory | awk '{print $3}')
echo "Current memory overcommit setting: $current_value"
case $current_value in
0) echo "Memory overcommit is DISABLED (default)" ;;
1) echo "Memory overcommit is ENABLED (Redis recommended)" ;;
2) echo "Memory overcommit is set to STRICT mode" ;;
esac
}
# Function to enable memory overcommit
enable_overcommit() {
check_root
echo "Enabling memory overcommit..."
# Set immediate effect
sysctl -w vm.overcommit_memory=1
# Make it permanent
if ! grep -q "vm.overcommit_memory = 1" /etc/sysctl.conf; then
echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
echo "Added permanent setting to /etc/sysctl.conf"
else
echo "Permanent setting already exists in /etc/sysctl.conf"
fi
echo "Memory overcommit has been enabled"
get_status
}
# Function to disable memory overcommit
disable_overcommit() {
check_root
echo "Disabling memory overcommit..."
# Set immediate effect
sysctl -w vm.overcommit_memory=0
# Remove from permanent configuration
sed -i '/vm.overcommit_memory = 1/d' /etc/sysctl.conf
echo "Removed permanent setting from /etc/sysctl.conf"
echo "Memory overcommit has been disabled"
get_status
}
# Main script
case "$1" in
enable)
enable_overcommit
;;
disable)
disable_overcommit
;;
status)
get_status
;;
*)
show_usage
;;
esac