Monthly Archives: 11月 2013

win nginx php 起動再起動シャットダウンのシェルファイル

SET NGINX_PATH=D:
SET NGINX_DIR=D:\web\nginx-1.3.11\
SET PHP_DIR=D:\web\php-5.4.11-nts-Win32-VC9-x86\

++++++++++++++++++ここから++++++++++++++++
cls
@ECHO OFF
SET NGINX_PATH=D:
SET NGINX_DIR=D:\web\nginx-1.3.11\
SET PHP_DIR=D:\web\php-5.4.11-nts-Win32-VC9-x86\

color 0a
TITLE Nginx+PHP5.4.11 プログラムの管理
GOTO MENU
:MENU
CLS
ECHO.
ECHO. * * * * * * * Nginx+PHP5.4.11 プログラムの管理 * * * * * *
ECHO. * *
ECHO. * 1 Nginx起動 *
ECHO. * *
ECHO. * 2  Nginxシャットダウン *
ECHO. * *
ECHO. * 3 Nginx 再起動 *
ECHO. * *
ECHO. * 4 php-cgi起動 *
ECHO. * *
ECHO. * 5 php-cgiシャットダウン *
ECHO. * *
ECHO. * 6 php-cgi再起動 *
ECHO. * *
ECHO. * 7 ログアウト *
ECHO. * *
ECHO. * * * * * * * * * * * * * * * * * * * * * * * *
ECHO.
ECHO.プロジェクトの番号を入力してください:
set /p ID=
IF “%id%”==”1” GOTO cmd1
IF “%id%”==”2” GOTO cmd2
IF “%id%”==”3” GOTO cmd3
IF “%id%”==”4” GOTO cmd4
IF “%id%”==”5” GOTO cmd5
IF “%id%”==”6” GOTO cmd6
IF “%id%”==”7” EXIT
PAUSE
:cmd1
ECHO.
ECHO.Nginx起動……
IF NOT EXIST %NGINX_DIR%nginx.exe ECHO %NGINX_DIR%nginx.exe不存在
%NGINX_PATH%
cd %NGINX_DIR%
IF EXIST %NGINX_DIR%nginx.exe start %NGINX_DIR%nginx.exe
ECHO.OK
PAUSE
GOTO MENU
:cmd2
ECHO.
ECHO.Nginxシャットダウン……
taskkill /F /IM nginx.exe > nul
ECHO.OK
PAUSE
GOTO MENU
:cmd3
ECHO.
ECHO.Nginxシャットダウン……
taskkill /F /IM nginx.exe > nul
ECHO.OK
GOTO cmd1
GOTO MENU
:cmd4
ECHO.
ECHO.php-cgi起動……
IF NOT EXIST %PHP_DIR%php-cgi.exe ECHO %PHP_DIR%php-cgi.exe存在しない
echo set wscriptObj = CreateObject(“Wscript.Shell”) >start_cgi.vbs
echo wscriptObj.run “%PHP_DIR%php-cgi.exe -b 127.0.0.1:9001”,0 >>start_cgi.vbs
start_cgi.vbs
del start_cgi.vbs
ECHO.OK
PAUSE
GOTO MENU
:cmd5
ECHO.
ECHO.php-cgiシャットダウン……
taskkill /F /IM php-cgi.exe > nul
ECHO.OK
PAUSE
GOTO MENU
:cmd6
ECHO.
ECHO.关闭php-cgi……
taskkill /F /IM php-cgi.exe > nul
ECHO.OK
GOTO cmd4
GOTO MENU

mysqlのバックアップ

#!/bin/bash
#backup mysql data
#Auther:arezone
#website:http://www.olinux.org.cn

DBDIR=/data/mysql
BACKDIR=/data/bak/mysql
TIME=date -d "today" +%F
DB=$(ls -p $DBDIR | grep / |tr -d / | grep -Ev ‘information_schema|performance_schema|mysql|test’)
/etc/init.d/mysqld stop
if [ -d $BACKDIR ]
then
echo “OK!”
else
mkdir -p $BACKDIR
fi
cd $DBDIR
for i in $DB
do
tar czf $BACKDIR/$i\_db\_$TIME.tar.gz $i
done
/etc/init.d/mysqld start
find $BACKDIR -name “*.tar.gz” -type f -mtime +15 -exec rm {} \; >/dev/null 2>&1

postfix log簡単整理

二つのファイルを処理する,一つは発送成功のケースです。もう一つは発送失敗場合、エラーログを書きこむ。

#!/bin/bash

#——————————————————-
#TITLE: Postfix Mail Log Handle
#AUTHOR: HankZhou
#TIME: 2013/07/17
#VERSION: 0.5V
#——————————————————-

#To determine whether a file exists
if [ ! -e $1 ]
then
printf “$1 No such file or directory\n\r”
exit 0
fi

dirName=”maillog”$(date +%y%m%d%H%M%S)

#To determine whether a directory exists
if [ -d $dirName ]
then
printf “Folder already exists\n\r”
exit 0
fi

mkdir $dirName

#Sented File handling
cat $1 | \
awk ‘{if(match($12,/^status=sent$/))
{mat=match($7,/[a-zA-Z0-9\_]+\@[a-zA-Z0-9\_\.]/);
lenMatch=length($7);
matched=substr($7,5,lenMatch-6);
timeOri=$1″ “$2” “$3;
cmd=”date \””timeOri”\” \”+%Y/%m/%d %T \””;
cmd|getline time;
lenTime=length(time);
timeH=substr(time,0,lenTime-1);
print timeH”,”matched “,Message accepted”}}’ | \
awk ‘BEGIN{FS=OFS=”,”}
{a[$2]++;a[$0]++;
if(a[$2]<2) {print $0}}' \ >./$dirName/sented.csv

#calculate number of lines
lines=$(awk ‘{b++;}END{print b}’ ./$dirName/sented.csv)
printf “**\n\rSented mail has been generated.\n\r”
printf “Totle of $lines lines\n\r”

#Error File handling
cat $1 | \
awk ‘{if(match($12,/^status=(deferred|bounced|expired)$/))
{mat=match($7,/[a-zA-Z0-9\_]+\@[a-zA-Z0-9\_\.]/);
lenMatch=length($7);
matched=substr($7,5,lenMatch-6);
timeOri=$1″ “$2” “$3;
cmd=”date -d \””timeOri”\” \”+%Y/%m/%d %T \””;
cmd|getline time;
lenTime=length(time);
timeH=substr(time,0,lenTime-1);
printf timeH”,”matched”,”;
printf substr($12,8);
for (i=13;i<=NF;i++){printf $i" "}; print "\n";}}' | \ awk 'BEGIN{FS=OFS=","} {a[$2]++;a[$0]++; if(a[$2]<2) {print $0}}' \ >./$dirName/ErrorMail.csv

lines=$(awk ‘{b++;}END{print b}’ ./$dirName/ErrorMail.csv)
printf “**\n\rError mail has been generated.\n\r”
printf “Totle of $lines lines\n\r”

Linuxコマンド一覧

1.あるポートの応用を殺す
port=9996
netstat -anp | grep $port | head -1 | awk ‘{print $7}’| awk ‘BEGIN{FS=”/”} {print $1}’ | xargs kill -9

2.Linuxの間にファイルを転送する
secure copy
scp [選択可のパラメータ] file_source file_target
scp local_file remote_username@remote_ip:remote_folder
scp local_file remote_username@remote_ip:remote_file
scp local_file remote_ip:remote_folder
scp local_file remote_ip:remote_file
scp -r local_folder remote_username@remote_ip:remote_folder
scp -r local_folder remote_ip:remote_folder

ftpで自動的にファイルをダウンロードする

cd /home/oracle/ftpdata
short_ymd=$(date +%y%m%d)
file=”10628110.20″”$short_ymd””.zip”
ftp -niv<

nginx サービス自動起動

cp nginx.sh /etc/init.d/nginx
chmod a+x /etc/init.d/nginx
chkconfig –add nginx
chkconfig nginx on
service nginx restart

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: – 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it’s not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog=”nginx”
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = “no” ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo “nginx already running….”
exit 1
fi
echo -n $”Starting $prog: ”
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $”Stopping $prog: ”
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $”Reloading $prog: ”
#kill -HUP cat ${nginx_pid}
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case “$1″ in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $”Usage: $prog {start|stop|restart|reload|status|help}”
exit 1
esac
exit $RETVAL

16位のunicode番号を読む

#!/bin/bash

cat /dev/stdin | while read -r line
do
for word in $line
do
/usr/bin/printf $word;
echo -n ” “;
done
echo “”;
done

HAproxy-Keepalived自動インストール配置shell

#!/bin/bash
##################################
haproxy_path=/usr/local/haproxy
haproxy_conf=/usr/local/haproxy/conf/haproxy.cfg
#############haproxy-install#####################
cd /tmp
tar xvf haproxy-1.4.*.tar.gz
if [ $? -eq 0 ];then
echo “haproxy tar ok”
else
echo “haproxy tar no”
exit
fi
sleep 5
yum install gcc openssl openssl-devel make kernel-devel popt-devel openssl -y
cd /tmp/haproxy-*
make TARGET=linux26 PREFIX=$haproxy_path install
if [ $? -eq 0 ];then
echo “haproxy install ok”
else
echo “haproxy install no”
exit
fi
cd $haproxy_path
mkdir conf logs
touch $haproxy_path/conf/haproxy.cfg
#################haproxy.cfg#########################
echo -e “global\n\tlog 127.0.0.1 local0\n\tmaxconn 65535\n\tchroot $haproxy_path\n\tuser nobody\n\tgroup nobody\n\tdaemon\n\tnbproc 8\n\tpidfile $haproxy_path/haproxy.pid\n” > $haproxy_conf

echo -e “\ndefaults\n\tlog 127.0.0.1 local3\n\tmode http\n\toption httplog\n\toption dontlognull\n\toption redispatch\n\tretries 2\n\tmaxconn 2000\n\tcontimeout 5000\n\tclitimeout 50000\n\tsrvtimeout 50000” >> $haproxy_conf

echo -e “\nlisten web 0.0.0.0:80\n\tmode http\n\tbalance roundrobin\n\tcookie SERVERID insert indirect nocache\n\toption httplog\n\tlog global\n\toption httpchk HEAD /index.html HTTP/1.0” >> $haproxy_conf

read -p “Requires several server ” server
for ((i=1;i<=$server;i++)) do read -p "server ip " ip echo -e "server server$i $ip:80 cookie $i check inter 2000 rise 2 fall 5" >> $haproxy_conf
done
###########################keepalived###############################
mandir=/usr/local/src/man
kernel=$(uname -r)
keepalived_conf=/etc/keepalived/keepalived.conf
################################
cd /tmp
tar -xvf keepalived*.tar.gz
cd /tmp/keepalived-*
#############keepalived make##################
./configure –prefix=/ –mandir=$mandir –with-kernel-dir=/usr/src/kernels/$kernel*
if [ $? -eq 0 ];then
make
if [ $? -eq 0 ];then
make install
if [ $? -eq 0 ];then
echo “keepalived ok”
else
echo “keepalived no”
exit
fi
else
echo “keeplaived make no”
exit
fi
else
echo “keelalived make no”
fi

touch /etc/keepalived/keepalived.conf
########################################################
read -p “NIC binding ” network
####################keepalived.conf#####################
#############Global configuration#######################
echo -e ” ! Configuration File for keepalived\nglobal_defs {\n\tnotification_email {\n\tjqbash@gmail.com\n\t}\n\tnotification_email_from root@localhost\n\tsmtp_server 127.0.0.1\n\tsmtp_connect_timeout 30\n\trouter_id HAproxy\n}” > $keepalived_conf
############Virtual IP Configuration####################
read -p “Requires several server ” server
for ((a=1;a<=$server;a++)) do read -p "MASTER of BACKUP " states read -p "priority " priority read -p "virtual_router id " id read -p "vip address " vip echo -e "vrrp_instance VI_$a {\n\tstate $states\n\tinterface $network\n\tvirtual_router_id $id\n\t priority $priority\n\tadvert_int 1\n\tauthentication {\n\tauth_type PASS\n\tauth_pass tdoa\n\t}\n\tvirtual_ipaddress {\n\t$vip/24 dev $network\n\t}\n}" >> $keepalived_conf
done
#######################################################
/usr/local/haproxy/sbin/haproxy -f $haproxy_conf
/etc/rc.d/init.d/keepalived restart
#########################haproxy-keepalived.sh##############################
touch /etc/rc.d/haproxy-keepalived.sh
echo -e ‘#!/bin/bash\nwhile\t:\ndo\n haproxypid=ps -C haproxy --no-header | wc -l\n\tif [ $haproxypid -eq 0 ];then\n\t\t/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg\n\t\tsleep 5\n\thapropid=ps -C haproxy --no-header | wc -l\n\t\tif [ $hapropid -eq 0 ];then\n\t\t\t\t/etc/init.d/keepalived stop\n\t\tfi\n\tfi\n\tsleep 5\n proxypid=ps -C haproxy --no-header | wc -l\n\tif (($proxypid!= 0));then\n\t\t/etc/init.d/keepalived start\n\tfi\n\tsleep 5\ndone’ > /etc/rc.d/haproxy.sh
#############################################################################
chmod +x /etc/rc.d/haproxy.sh
/usr/bin/nohup /etc/rc.d/haproxy.sh &
rc_local=/etc/rc.local
echo ‘/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg’ >> $rc_local
echo ‘/etc/rc.d/init.d/keepalived start’ >> $rc_local
echo ‘/usr/bin/nohup /etc/rc.d/haproxy.sh &’ >> $rc_local
############################################################################

バッチ処理でn階段の魔方陣を出力する

@echo off
:start
set /p n=Please input your odd integer:
set /a p=n%%2
if %p% equ 0 echo Input error!&&goto start

set /a i=%n%
set /a t=(%n%+1)/2
set /a a%t%_%n%=1,temp=1,m=n-1,p=n
:repeat
set /a p-=1
if %p% lss 0 goto array
for /l %%a in (1 1 %m%) do (
setlocal enabledelayedexpansion
if !t! gtr %n% set /a t=1
if !i! gtr %n% set /a i=1
set /a s=!t!+1,q=!i!+1
if !s! gtr %n% set /a s=1
if !q! gtr %n% set /a q=1
set /a a!s!_!q!=a!t!_!i!+1,t+=1,i+=1
set /a temp+=1
if !temp! equ %n% (
set /a temp=1,tmp=a!s!_!q!,q-=1,i-=1
if !q! equ 0 set /a q=n
set /a a!s!_!q!=tmp+1
goto repeat
)
)
pause
:array
echo Now behold the powerful Magic Sqaure^^!:
set /a a!t!_!n!=1
for /l %%c in (!n! -1 1) do (
for /l %%d in (1 1 !n!) do (
set /a result=a%%d_%%c
set /p=!result!nul

簡単なプロセス、ディスク、ログ監視用のシェルスクリプト

#!/bin/bas
#
################################################################################
# process status monitor config
################################################################################
# process num
ProcNum=2
#
ProcUser[0]=sybase
ProcName[0]=dataserver
#
ProcUser[1]=sybiq
ProcName[1]=iqsrv15
#
#
################################################################################
# filesystem space monitor config
################################################################################
# filesystem space num
FsNum=1
#
# FsName: filesystem name
# MaxPercent: maximum filesystem space used
FsName[0]=/home/roo/
MaxPercent[0]=90
#
#
################################################################################
# program log monitor config
################################################################################
# LogNum:log num
LogNum=1
#
# LogDir: log file directory
# LogName: log file name
# MaxHoldMin: warning if log file doesn’t increase in appointed minutes
LogDir[0]=/home/roo/
LogName[0]=$(date +%Y%m%d).log
MaxHoldMin[0]=10
#
#
################################################################################
# function declaration
################################################################################
# function get process id
function GetPID #User #Name
{
PsUser=$1
PsName=$2
pid=$(ps -u $PsUser|grep $PsName|grep -v grep|grep -v vi|grep -v dbx \
|grep -v tail|grep -v start|grep -v stop |sed -n 1p |awk ‘{print $1}’)
echo $pid
}
#
# function get filesystem space
function GetDiskSpc #FileSystem
{
if [ $# -ne 1 ]; then
return 1
fi
#
FsDir=$1
DiskSpace=$(df -P $FsDir |tail -1 |awk ‘{print $5}’ |awk -F% ‘{print $1}’)
echo $DiskSpace
}
#
#
function GetLogHold #LogPath
{
LogTime=$(ls –full-time $LogPath | awk ‘{print $6″ “$7}’ | cut -d”.” -f1)
SYSTime=$(date +%F” “%T)
echo | \
awk -v T1=LogTime -v T2=SYSTime ‘{
t1=mktime(T1);
t2=mktime(T2);
dif=(t2-t1)/60;
print dif}’
}
#
################################################################################
# process check
################################################################################
echo
echo “– PROCESS STATUS CHECK BEGIN.”
i=0
while [ $i -lt $ProcNum ]
do
echo -n “Check process ${ProcName[$i]}: ”
PID=$(GetPID ${ProcUser[$i]} ${ProcName[$i]})
#
if [ “-$PID” == “-” ]; then
echo “Warning: process ${ProcName[$i]} does not exist!”
else
echo “Process ${ProcName[i]} is normal.”
fi
#
echo
i=$[ $i + 1 ]
done
#
echo “– PROCESS STATUS CHECK END.”
echo
echo
#
################################################################################
# file system spaces check
################################################################################
echo
echo “– FILESYSTEM SPACE CHECK BEGIN.”
i=0
while [ $i -lt $FsNum ]
do
echo -n “Check filesystem ${FsName[$i]}: ”
DiskSpace=$(GetDiskSpc ${FsName[$i]})
#
if [ $DiskSpace -gt ${MaxPercent[$i]} ]; then
echo “Warning: filesystem (${FsName[$i]}) used $DiskSpace% more than ${MaxPercent[$i]}%!”
else
echo “Filesystem [${FsName[$i]}] used $DiskSpace%.”
fi
#
echo
i=$[ $i + 1 ]
done
#
echo “– FILESYSTEM SPACE CHECK END.”
echo
echo
#
################################################################################
# program log check
################################################################################
echo
echo “– LOG STATUS CHECK BEGIN.”
i=0
while [ $i -lt $LogNum ]
do
echo -n “Check log ${LogDir[i]}${LogName[i]} : ”
LogHoldMin=$(GetLogHold ${LogDir[i]}${LogName[i]})
#
if [ $LogHoldMin -gt ${MaxHoldMin[0]} ]; then
echo “Warning: log [${LogDir[i]}${LogName[i]}] hold $LogHoldMin minutes more than ${MaxHoldMin[0]}!”
else
echo “Log [${LogDir[i]}${LogName[i]}] hold $LogHoldMin minutes.”
fi
#
echo
i=$[ $i + 1 ]
done
#
echo “– LOG STATUS CHECK END.”
echo
echo
#