-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcleanRAM.sh
More file actions
40 lines (30 loc) · 1.08 KB
/
cleanRAM.sh
File metadata and controls
40 lines (30 loc) · 1.08 KB
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
#!/bin/bash
#author: salman sk
config_file="config.conf"
log_file="logs/memory_cleanup.log"
# Ensure log directory exists
log_dir=$(dirname "$log_file")
mkdir -p "$log_dir"
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
echo "You have to run this script with superuser privileges." | tee -a "$log_file"
exit 1
fi
if [ ! -f "$config_file" ]; then
echo "Config file not found. Exiting." | tee -a "$log_file"
exit 1
fi
source "$config_file"
# Get memory information
MEM_FREE=$(grep MemFree /proc/meminfo | tr -cd '[:digit:]')
MEM_FREE_MIB=$(expr $MEM_FREE / 1024)
# Output information
echo -e "This script will clean your RAM.\nNow you have $MEM_FREE_MIB MiB available memory." | tee -a "$log_file"
# Clean RAM
sync; echo 3 > /proc/sys/vm/drop_caches
# Get memory info after cleaning
AFTER_MEM_FREE=$(grep MemFree /proc/meminfo | tr -cd '[:digit:]')
AFTER_MEM_FREE_MIB=$(expr $AFTER_MEM_FREE / 1024)
# Output information
CLEANED_MIB=$(expr $AFTER_MEM_FREE_MIB - $MEM_FREE_MIB)
echo "This cleaned $CLEANED_MIB MiB. Now you have $AFTER_MEM_FREE_MIB MiB." | tee -a "$log_file"