Auto restart server if virtual memory is too low

  1. Check how much memory is left on the VPS.
  2. If 500M memory is left , reboot the VPS.
This could be done as follow
  1. Write a script that checks how much memory is left and reboot the VPS
  2. Add this script to crontab and for automatization
e.g
#!/bin/bash

mem=$(free -m | awk '/Mem:/{print $4}')

(( mem <= 500 )) && reboot
Make the script executable
chmod +x scriptname // note don't add an extension
Add the script to the cron
crontab -e
* * * * * user_to_run_the_script /path/to/the/script

Post a Comment