Script Auto restart

สร้าง file

cd /usr/local/
mkdir auto
cd auto
nano restart.sh

ส่วน Script  มีให้เลือก 3 แบบลองใช้งานดู

สำหรับ Directadmin

#!/bin/bash

# Hostname
URL=”http://domian.com”

# MySQL
USER=”da_admin”
PASSWD=$(grep “^passwd=” /usr/local/directadmin/conf/mysql.conf | cut -d= -f2)

# CMD
WORK=”200″
NGINX=$(curl –write-out %{http_code} –silent –output /dev/null $URL)
HTTPD=$(curl –write-out %{http_code} –silent –output /dev/null $URL’:8085′)
mysql –user=”${USER}” –password=”${PASSWD}” -e exit 2>/dev/null
SQLSTATUS=`echo $?`

# Auto restart nginx
if [ $NGINX -ne $WORK ]; then
echo Nginx response code $NGINX
/etc/init.d/nginx restart
fi;

# Auto restarrt httpd
if [ $HTTPD -ne $WORK ]; then
echo Nginx response code $NGINX
/etc/init.d/httpd restart
fi;

if [ $SQLSTATUS -ne “0” ]; then
echo MySQL is down!
/usr/bin/killall mysqld
/etc/init.d/mysqld restart
fi;

สำหรับ MySQL,NginX,php-fpm

#!/bin/bash
#Scripts to start services if not running
ps -ef | grep nginx |grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/nginx start > /dev/null
fi
ps -ef | grep php5-fpm |grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/php5-fpm start > /dev/null
fi
ps -ef | grep mysql |grep -v grep > /dev/null
if [ $? != 0 ]
then
/etc/init.d/mysql start > /dev/null
fi

สำหรับ Apache

#เช็คจาก Server Load
#!/bin/sh
check=`cat /proc/loadavg | sed ‘s/\./ /’ | awk ‘{print $1}’`
if [ $check -gt 10 ] //10 is load average on 5 minutes
then
/etc/init.d/httpd restart
fi

#เช็คจาก http process
#!/bin/sh
check=`ps aux | grep ‘httpd’ | awk ‘{print $6;}’ | awk ‘{avg += $1;} END {printf avg;}’`
if [ $check -gt 200000 ]
then
/etc/init.d/httpd restart
/etc/init.d/nginx restart
/etc/init.d/php-fpm56 restart
/etc/init.d/php-fpm70 restart
printf $check
fi

แก้ไข permision

chmod +x restart.sh

สร้าง cron

cd ~
cd /etc/cron.d
nano auto_restart
*/5 * * * * root /usr/local/auto/restart.sh >/dev/null 2>&1