Every Linux System has three options to clear cache without interrupting any processes or services.
1. Clear PageCache only.
# sync; echo 1 > /proc/sys/vm/drop_caches
2. Clear dentries and inodes.
# sync; echo 2 > /proc/sys/vm/drop_caches
3. Clear pagecache, dentries, and inodes.
# sync; echo 3 > /proc/sys/vm/drop_caches
Now we will be creating a shell script to auto clear RAM cache daily at 2 am via a cron scheduler task. Create a shell script clearcache.sh and add the following lines.
#!/bin/bash
# Note, we are using "echo 3", but it is not recommended in production instead use "echo 1"
echo "echo 3 > /proc/sys/vm/drop_caches"
Set execute permission on the cache.sh file.
# chmod 755 cache.sh
Now you may call the script whenever you are required to clear the ram cache.
Now set a cron to clear RAM cache every day at 2 am. Open crontab for editing.
# crontab -e
Append the below line, save and exit to run it at 2 am daily.
0 2 * * * /path/to/cache.sh