Linux下用的脚本
http://blog.itpub.net/29510932/viewspace-1166603/
批量启动Tomcat
点击(此处)折叠或打开
- #!/bin/bash
- #JDK路径
- export JAVA_HOME="/usr/jdk1.7.0_09"
- #前缀路径
- PREFIX="/data/webapp/apache-tomcat-80"
- #起始编号
- BEGIN=1
- #终止编号
- END=20
- sudo chown -R ccapp:ccapp /data/webapp/apache-tomcat-80*
- sudo chown -R ccapp:ccapp /usr/logs/*
- #关闭
- stop(){
- for((i=$BEGIN;i<=$END;i++))
- do
- #抽取Tomcat进程号PID
- #pid=`ps -ef | grep tomcat7-$i |grep -v 'grep' | awk '{print $2}'`
- #测试当前进程是否处于活动状态
- #test "$pid" != "" && kill $pid && echo "Stop tomcat7-$i Successful!" || echo "Stop tomcat7-$i Failed!"
- if [ $i -lt 10 ]; then
- #echo "tomcat7-0$i"
- pid=`ps -ef | grep apache-tomcat-800$i/ |grep -v 'grep' | awk '{print $2}'`
- test "$pid" != "" && sudo kill $pid && echo "Stop apache-tomcat-800$i Successful!" || echo "Stop apache-tomcat-800$i Failed!"
- else
- #echo "tomcat7-$i"
- pid=`ps -ef | grep apache-tomcat-80$i/ |grep -v 'grep' | awk '{print $2}'`
- test "$pid" != "" && sudo kill $pid && echo "Stop apache-tomcat-80$i Successful!" || echo "Stop apache-tomcat-80$i Failed!"
- fi
- sleep 1
- done
- #echo
- }
- #启动
- start(){
- for((i=$BEGIN;i<=$END;i++))
- do
- #启动Tomcat
- #$PREFIX$i/bin/startup.sh && echo "Start tomcat7-$i Successful!"
- #echo
- if [ $i -lt 10 ]; then
- #echo "tomcat7-0$i"
- $PREFIX\0$i/bin/startup.sh && echo "Start apache-tomcat-800$i Successful!"
- else
- #echo "tomcat7-$i"
- $PREFIX$i/bin/startup.sh && echo "Start apache-tomcat-80$i Successful!"
- fi
- sleep 1
- done
- }
- status(){
- for((i=$BEGIN;i<=$END;i++))
- do
- #抽取Tomcat进程号PID
- #pid=`ps -ef | grep tomcat7-$i |grep -v 'grep' | awk '{print $2}'`
- #打印PID
- #echo "tomcat7-$i - $pid"
- if [ $i -lt 10 ]; then
- #echo "tomcat7-0$i"
- #$PREFIX\810$i/bin/startup.sh && echo "Start tomcat7-810$i Successful!"
- pid=`ps -ef | grep apache-tomcat-800$i/ |grep -v 'grep' | awk '{print $2}'`
- echo "apache-tomcat-800$i - $pid"
- else
- #echo "tomcat7-$i"
- #$PREFIX\81$i/bin/startup.sh && echo "Start tomcat7-81$i Successful!"
- pid=`ps -ef | grep apache-tomcat-80$i/ |grep -v 'grep' | awk '{print $2}'`
- echo "apache-tomcat-80$i - $pid"
- fi
- done
- }
- case "$1" in
- start)
- #echo "Start tomcat7-$BEGIN ~ tomcat7-$END"
- #echo
- start
- #echo
- ;;
- stop)
- #echo "Stop tomcat7-$BEGIN ~ tomcat7-$END"
- #echo
- stop
- #echo
- ;;
- restart)
- #echo "Restart tomcat7-$BEGIN ~ tomcat7-$END"
- #echo
- stop
- sleep 2
- start
- #echo
- ;;
- status)
- #echo "Pid of tomcat7-$BEGIN ~ tomcat7-$END"
- status
- #echo
- ;;
- *)
- echo $"Usage: $0 {start|stop|restart|status}"
- exit 1
- ;;
- esac
Nginx自检脚本(每60秒自动检测一次)
点击(此处)折叠或打开
- #!/bin/bash
- prefix=/usr/local/nginx_1.4.6/sbin
- while :
- do
- nginxpid=`ps -C nginx --no-header | wc -l`
- if [ $nginxpid -eq 0 ]; then
- DATE=$(date +%Y-%m-%d %T)
- killall nginx && $prefix/nginx && echo "$DATE Restart Nginx successful~" || echo "$DATE Fail to restart Nginx!"
- sleep 60
- else
- DATE=$(date +%Y-%m-%d %T)
- echo "$DATE : Nginx_Check is running, everything is OK~"
- sleep 60
- fi
- done
业务部署自动化脚本
点击(此处)折叠或打开
- #!/bin/bash
- #脚本说明
- # 内容分发至各实例
- #变更记录
- #
- #系统环境变量
- PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
- export PATH
- #自定义变量
- bak=`date +%Y%m%d-%T`
- prefix=/usr/
- #tomcat_path=/usr/apache-tomcat-7.0.33/bin
- project=APP
- #begin=1
- #end=15
- #实例备份
- cd $prefix
- mkdir -p /home/user/$project/$bak
- \cp -rf $project /home/user/$project/$bak
- #根据实际情况修改,脚本参数如APP.zip解压后可以为APP的文件夹
- #也可以是APP文件夹下的内容
- cd /tmp/user/
- rm -rf ${project}
- mkdir $project
- cp $project\.war ./$project
- cd $project
- unzip ${project}\.war
- #unzip $1
- cd ..
- #chmod -R 755 $project
- #分发copy,如果所有的工程都指向一个文件夹,则无需分发,直接拷贝
- #for((i=$begin;i<=$end;i++))
- #do
- #根据需要增加覆盖copy的项目
- # \cp -rf $project $prefix-$i/webapps/
- #done
- #rm -rf $prefix$project
- cp -avpf $project\/* $prefix$project\/
- #Tomcat操作留给其他功能模块控制
- #cd $tomcat_path
- #./shutdown.sh
- #./startup.sh
- echo "update successful!"
懒人用删除Tomcat日志脚本(其实什么日志都能删)
点击(此处)折叠或打开
- #!/bin/bash
- #前缀路径
- PREFIX="/usr/TomcatSvr7-"
- #起始编号
- BEGIN=2
- #终止编号
- END=20
- for((i=$BEGIN;i<=$END;i++))
- do
- #删除历史记录
- rm -rf $prefix$i/logs/*0*
- cat /dev/null > $prefix$i/logs/catalina.out
- done
Nginx日志管理
点击(此处)折叠或打开
- logs_path=\"/usr/local/nginx_1.4.6/logs/\"
- mv ${logs_path}www.log ${logs_path}www_$(date -d \"yesterday\" +\"%Y%m%d\").log
- mv ${logs_path}error.log ${logs_path}error_$(date -d \"yesterday\" +\"%Y%m%d\").log
- kill -HUP `cat ${logs_path}nginx.pid`
- find $logs_path -name \"www_*log\" -mtime +0 -exec rm -rf {} ;
- #find $logs_path -name \"access_*log\" -mtime +2 -exec rm -rf {} ;
- find $logs_path -name \"error_*log\" -mtime +0 -exec rm -rf {} ;
在root下输入crontab -e , 01 00 * * * /home/nginxlog.sh,指定每天0:01分自动执行脚本~
自动配置LVM分区
点击(此处)折叠或打开
- #!/bin/bash
- DEV=/dev/sdb
- VG=VG_DATA
- LG=lv_data
- DIR=/data
- pvcreate $DEV \
- && vgcreate $VG $DEV \
- && lvcreate -l 100%PVS -n $LG $VG \
- && mkfs.ext4 /dev/$VG/$LG \
- && mkdir $DIR
- && mount /dev/$VG/$LG $DIR
Nginx配置
点击(此处)折叠或打开
- server_tokens off;
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 65;
- client_header_timeout 10;
- client_body_timeout 10;
- reset_timedout_connection on;
- send_timeout 10;
- charset utf-8;
- server_names_hash_bucket_size 128;
- client_header_buffer_size 512k;
- large_client_header_buffers 4 64k;
- client_max_body_size 16m;
- client_body_buffer_size 256k;
- proxy_connect_timeout 60;
- proxy_send_timeout 30;
- proxy_read_timeout 30;
- proxy_buffer_size 16k;
- proxy_buffers 4 256k;
- proxy_busy_buffers_size 512k;
- proxy_temp_file_write_size 512k;
- open_file_cache max=10240 inactive=20s;
- open_file_cache_valid 30s;
- open_file_cache_min_uses 10;
- open_file_cache_errors on
Tomcat连接池配置
点击(此处)折叠或打开
- port="5000"
- protocol="org.apache.coyote.http11.Http11NioProtocol"
- connectionTimeout="30000"
- URIEncoding="UTF-8"
- maxThreads="1000"
- minSpareThreads="100"
- acceptCount="300"
- acceptorThreadCount="6"
- keepAliveTimeout="120000"
- maxKeepAliveRequests="1000"
- enableLookups="false"
- redirectPort="8443" />
实时查看连接数
点击(此处)折叠或打开
- watch -n 1 "netstat -n | awk '/^tcp/ {++S[\$NF]} END {for(a in S) print a, S[a]}'"
远程切换Nginx
点击(此处)折叠或打开
- #!/bin/bash
- PORT1=8066
- PORT2=10066
- switch_to_8066(){
- ssh 10.10.10.11 "/bin/bash /root/nginx_p$PORT1.sh;exit"
- }
- switch_to_10066(){
- ssh 10.10.10.11 "/bin/bash /root/nginx_p$PORT2.sh;exit"
- }
- switch_to_all(){
- ssh 10.10.10.11 "/bin/bash /root/nginx_all.sh;exit"
- }
- case "$1" in
- switch_to_8066)
- echo "switch nginx to port $PORT1"
- switch_to_8066
- echo "switch to port $PORT1 success"
- ;;
- switch_to_10066)
- echo "switch nginx to port $PORT2"
- switch_to_10066
- echo "switch to port $PORT2 success"
- ;;
- switch_to_all)
- echo "switch nginx to port $PORT1 and $PORT2"
- switch_to_all
- echo "switch to port $PORT2 and $PORT1 success"
- ;;
- *)
- echo $"Usage: $0 {switch_to_$PORT1|switch_to_$PORT2|switch_to_all}"
- exit 1
- ;;
- esac
被远程调用的脚本,只有红色部分有区别
点击(此处)折叠或打开
- #!/bin/bash
- NGINX_CONF=/usr/local/nginx_1.8.0/conf/
- NGINX_SBIN=/usr/local/nginx_1.8.0/sbin/nginx
- CONF_FILE=nginx_8066.conf
- ps -ef | grep nginx |grep -v 'grep' |grep -v 'nginx_p8066.sh' | awk '{print $2}' | xargs kill
- $NGINX_SBIN -c $NGINX_CONF$CONF_FILE
查看磁盘的TPS(每秒数据交换次数)
点击(此处)折叠或打开
- iostat -d sda | grep sda | awk '{ print $2; }'
- iostat -d | tail -n +4 | head -n -1 | awk '{s+=$2} END {print s}'
SVN自动发布(捡漏)
点击(此处)折叠或打开
- #!/bin/bash
- REMOTE_HOST1=192.168.1.100
- REMOTE_USER1=
- REMOTE_PWD1=
- DIR=TEST
- PROJECT=TEST_PRO
- BRANCH=trunk
- SVN_URL=http://192.168.1.233/svn/
- LOG=svn_file_change.log
- TMP_DIR=/home/test/
- TARGET_DIR=/data/save/
- if [ ! -d $TMP_DIR$DIR ]; then
- mkdir $TMP_DIR$DIR
- else
- echo "svn files is exist, script start......time : $(date -d today +%Y-%m-%d\ %H:%M:%S)" >> $TMP_DIR$LOG
- #rm -rf $TMP_DIR$DIR && echo "the old dir is deleted : $TMP_DIR$FILE, time : $(date -d today +%Y-%m-%d\ %H:%M:%S)" >> $TMP_DIR$LOG
- if [ $? -eq 0 ]; then
- svn co $SVN_URL$PROJECT/$BRANCH/$DIR
- if [ $? -eq 0 ]; then
- expect -c "
- spawn ssh $REMOTE_USER1@$REMOTE_HOST1 \"rm -rf $TARGET_DIR$DIR\"
- expect {
- \"*assword\" {set timeout 300; send \"$REMOTE_PWD1\r\";}
- \"yes/no\" {send \"yes\r\"; exp_continue;}
- }
- expect eof"
- expect -c "
- spawn scp -r $TMP_DIR$DIR $REMOTE_USER1@$REMOTE_HOST1:$TARGET_DIR
- expect {
- \"*assword\" {set timeout 300; send \"$REMOTE_PWD1\r\";}
- \"yes/no\" {send \"yes\r\"; exp_continue;}
- }
- expect eof"
- expect -c "
- spawn ssh $REMOTE_USER1@$REMOTE_HOST1 \"chown -R test:test $TARGET_DIR$DIR\"
- expect {
- \"*assword\" {set timeout 300; send \"$REMOTE_PWD1\r\";}
- \"yes/no\" {send \"yes\r\"; exp_continue;}
- }
- expect eof"
- #rm -rf $TMP_DIR$DIR
- else
- echo "svn error, can not copy the file!......" >> $TMP_DIR$LOG
- fi
- else
- echo "Unkown error......" >> $TMP_DIR$LOG
- fi
- fi
VIP
点击(此处)折叠或打开
- /sbin/ip addr add 100.100.100.100/24 dev eth0
- /sbin/ip addr del 100.100.100.100/24 dev eth0