0% found this document useful (0 votes)
22 views

Automation Script and Explanation

The document discusses a Bash script that automates cleaning of cache memory on a Linux server. The script uses commands like free, sync and echo to purge cache levels and generate a report on memory usage before and after cleaning. It also provides instructions to schedule the script weekly using crontab.

Uploaded by

thousifshaik996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Automation Script and Explanation

The document discusses a Bash script that automates cleaning of cache memory on a Linux server. The script uses commands like free, sync and echo to purge cache levels and generate a report on memory usage before and after cleaning. It also provides instructions to schedule the script weekly using crontab.

Uploaded by

thousifshaik996
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Automation for cleaning Cache memory: Post 2 of 5

Script
#!/bin/bash
free -wh > ~/old
purging () {
sudo sync; sudo echo 1 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 1 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 1 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 2 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 2 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 2 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 3 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 3 > /proc/sys/vm/drop_caches
sudo sync; sudo echo 3 > /proc/sys/vm/drop_caches
}
report(){
echo -e "\n\n\n\nServer $(curl -s -4 ident.me;echo)'s cache Purging report:-\n-----Before Purging--
--\n$(cat ~/old)\n\n-----After Purging-----\n$(free -wh)\nTimeStamp:- $(TZ='Asia/Kolkata'
date)\n\n"
}
purging > /dev/null 2>&1 && report || echo -e "Kindly run with sudo rights\nExample:- sudo
mfree"

Explanation

Line 1: #!/bin/bash

• This is the shebang line, which tells the operating system to use the Bash shell to
interpret and execute the following script.

Line 2: free -wh > ~/old


• This line executes the free command with options -w (human-readable) and -
h (kilobytes).

• The output is redirected to a file named old in the user's home directory.

• This creates a snapshot of memory usage before the purging process, serving as
a reference point for comparison.

Line 3: purging () { ... }

• This defines a function named purging.

• The function body contains multiple lines:

o sudo sync; sudo echo 1 > /proc/sys/vm/drop_caches: This line attempts


to free various levels of cached memory by:

▪ sudo sync: Forces the system to write all data from memory to
disk.

▪ sudo echo 1 > /proc/sys/vm/drop_caches: Tells the kernel to drop


unused page cache entries.

▪ This command is repeated multiple times with values 2 and


3, aiming to progressively free more memory. However, it's crucial
to understand that this can significantly impact system
performance if done aggressively.

Line 12: report () { ... }

• This defines a function named report.

• The function constructs a multi-line report containing:

o Server identification using curl and ident.me.

o Memory usage before purging, retrieved from the old file.

o Memory usage after purging, obtained by running free.

o Timestamp formatted for India timezone (Asia/Kolkata).

Line 16: purging > /dev/null 2>&1 && report || echo -e "Kindly run with sudo
rights\nExample:- sudo mfree"

• This line attempts to run the purging function.

o > /dev/null: Discards the standard output, preventing it from cluttering the
terminal.
o 2>&1: Redirects standard error to standard output, allowing both outputs
to be handled together.

• If the function execution succeeds (&&), the report function is called to generate
a report.

• If the function fails due to insufficient permissions (||), an error message


explaining the need for sudo rights and an example usage is displayed.

Important Cautions:

• Execute with caution: Aggressive cache clearing can negatively impact system
performance. Consider alternative approaches like targeted cache invalidation
or memory management tools.

• Understand the implications: Executing scripts with sudo grants elevated


privileges. Thoroughly understand the script's behaviour and potential
consequences before running it.

• Seek professional guidance: Complex system administration tasks may require


expert intervention. If unsure, consult a qualified professional.

If you want to execute this script on every Sunday at 6pm then you can update like this
in crontab:
0 18 * * 0 /path/to/your/script.sh

You can customize it as per your need.

Explanation:

0: Minute (0 represents 6pm)


18: Hour (24-hour format; 18 corresponds to 6pm)
*: Day of month (ignored since we specify day of week)
*: Month (ignored since we specify day of week)
0: Day of week (0 or 7 both represent Sunday)
/path/to/your/script.sh: The path to your script file

Follow Shivam Agnihotri on LinkedIn for useful DevOps Content.

Join DevOps Ocean Group: https://www.linkedin.com/groups/9189158/


If you are looking for a Dedicated 1:1 session with me to boost your DevOps
Productivity, then please book a session from here: https://topmate.io/shivam_agnihotri

You might also like