Hi,
I have been working on a recycle script for my server which will restart the box when the memory drops below 10%, my script writing skills suck, so I thought I would ask for some help, you can see my effort so far below, the COMMAND string there will when entered in SSH show the remaining free memory in kb, howevery in my shell script it just complains that "No Such File or directory", so I must be doing something wrong!
Any help would be appreciated!
Code:
#!/bin/bash
COMMAND="cat /proc/meminfo|grep \"MemFree*\"|grep -oE \"[[:digit:]]{1,}\"| awk '{print $1}'"
FREEMEM=$COMMAND
echo $FREEMEM
if [ $FREEMEM <= 200000 ] # if mysql not running
then
echo "Memory lower then 10%, so we kill and restart"
else
echo "Memory is fine"
fi
I got apache, mysql, and courier restarting when it is not in the process list, so I managed something, and before you mention "monit" I installed and tried that but it fails to start on my box just saying "starting monit daemon ...." so I figured stick to shell scripts in crontab for now.
Below are the apache, mysql, and courier scripts if anyone is interested.
Apache
Code:
#!/bin/bash
RESTART="/etc/init.d/apache2 restart"
#path to pgrep command
PGREP="/usr/bin/pgrep"
# Httpd daemon name,
# Under Debian 4.x it is apache2
#HTTPD="httpd"
HTTPD="apache2"
# find httpd pid
$PGREP ${HTTPD}
if [ $? -ne 0 ] # if apache not running
then
# restart apache
$RESTART
fi
MySQL
Code:
#!/bin/bash
RESTART="/etc/init.d/mysql restart"
#path to pgrep command
PGREP="/usr/bin/pgrep"
# mysql daemon name,
# Under Debian 4.x it is mysqld
PROCESS="mysqld"
# find mysql pid
$PGREP ${PROCESS}
if [ $? -ne 0 ] # if mysql not running
then
# restart mysql
$RESTART
fi
Courier
Code:
#!/bin/bash
RESTART="/etc/init.d/courier-imap restart"
#path to pgrep command
PGREP="/usr/bin/pgrep"
# courier daemon name,
PROCESS="couriertcpd"
# find courier pid
$PGREP ${PROCESS}
if [ $? -ne 0 ] # if courier not running
then
# restart courier
$RESTART
fi
Bookmarks