To view the buff/cache:

  1. free -h

image.png

To clean the buff/cache:

  1. echo 1 > /proc/sys/vm/drop_caches
  2. echo 2 > /proc/sys/vm/drop_caches
  3. sync && echo 3 > /proc/sys/vm/drop_caches

You can put these shell commands in a script and run it in schedule with crontab:

Firstly, create script named clean.sh.

  1. #!/bin/bash
  2. echo 1 > /proc/sys/vm/drop_caches
  3. echo 2 > /proc/sys/vm/drop_caches
  4. sync && echo 3 > /proc/sys/vm/drop_caches

Secondly, run the script with crontab.

  1. # Run commands go into editor mode.
  2. crontab -e
  3. # Enter editor mode and input below content with vim command.
  4. # Execute once every 4 hours.
  5. 0 */2 * * * sh /your/path/clean.sh
  6. # View the script if it exists or not.
  7. crontab -l
  8. # Clean the script if exist.
  9. crontab -r