How to add a RAM free up script into Raspbian for a Raspberry PI 2 b+

How to add a RAM free up script into Raspbian for a Raspberry PI 2 b+

On this time we create a script using a cronjob to free up RAM in our Raspberry PI, it will run every 5 hours. I made this script because I realized that Raspberry PI freezes sometimes and it's because the device it's ran out of memory.

In your home folder or where you want to create the script, you start creating the file, in the terminal we use

touch free-ram.sh && chmod +x free-ram.sh

Open the file using vim or whatever you use to edit files

vim free-ram.sh

We add this script into the file, this script generates a log file also, it's in order to check the free RAM after runs the script

#!/bin/bash
FILE=/var/log/free-ram.log
sync
echo 3 > /proc/sys/vm/drop_caches

if test -f "$FILE"; then
    echo -e "$(date) \n$(egrep 'MemFree|MemAvailable' /proc/meminfo)"  >> /var/log/free-ram.log
else
    touch /var/log/free-ram.log
    echo -e "$(date) \n$(egrep 'MemFree|MemAvailable' /proc/meminfo)" >> /var/log/free-ram.log
fi

You can test the script using

sudo ./free-ram.sh

After the script running process is finished you can check the results inside the log file created on "/var/log/free-ram.log"

tail /var/log/free-ram.log

You can see the results

Fri 26 Aug 11:17:18 -04 2022 
MemFree:          539644 kB
MemAvailable:     656820 kB

After that we can procede to add the new task on the cronjob list

First you need to switch the user to the root user

sudo su

Then open the crontab editor with

crontab -e

You can something like this

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command

At the end of the file you will add the cronjob task using the absolute path of your script in my case my script is on my home folder "/home/pi/projects/scripts/free-ram.sh"

The file will look like this after the modification

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
0 */5 * * * /home/pi/projects/scripts/free-ram.sh

And that's it, the script will free up ram every 5 hours and you can check the logs inside the logs folder.