My server is centos 5.7. I need a script work with cron job that auto restarts mysql service once the memory usage reaches 55%.
Is below script correct?
#!/bin/bash
TOTAL=`free | grep Mem | awk '{print $2}'`
USED=`free | grep buffers\/cache | awk '{print $3}'`
let PERCENT=$USED*100/$TOTAL
if [ $PERCENT -gt 55 ]; then
service mysqld restart
fi
Yes, it’s basic but correct.
A more complete and robust approach would be to just install monit, which can perform actions on conditions like this and also provides other services.