Linux下用的脚本

http://blog.itpub.net/29510932/viewspace-1166603/

 

批量启动Tomcat

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #JDK路径
  3. export JAVA_HOME="/usr/jdk1.7.0_09"
  4. #前缀路径
  5. PREFIX="/data/webapp/apache-tomcat-80"
  6. #起始编号
  7. BEGIN=1
  8. #终止编号
  9. END=20
  10. sudo chown -R ccapp:ccapp /data/webapp/apache-tomcat-80*
  11. sudo chown -R ccapp:ccapp /usr/logs/*
  12. #关闭
  13. stop(){
  14. for((i=$BEGIN;i<=$END;i++))
  15. do
  16. #抽取Tomcat进程号PID
  17. #pid=`ps -ef | grep tomcat7-$i |grep -v 'grep' | awk '{print $2}'`
  18. #测试当前进程是否处于活动状态
  19. #test "$pid" != "" && kill $pid && echo "Stop tomcat7-$i Successful!" || echo "Stop tomcat7-$i Failed!"
  20. if [ $i -lt 10 ]; then
  21. #echo "tomcat7-0$i"
  22. pid=`ps -ef | grep apache-tomcat-800$i/ |grep -v 'grep' | awk '{print $2}'`
  23. test "$pid" != "" && sudo kill $pid && echo "Stop apache-tomcat-800$i Successful!" || echo "Stop apache-tomcat-800$i Failed!"
  24. else
  25. #echo "tomcat7-$i"
  26. pid=`ps -ef | grep apache-tomcat-80$i/ |grep -v 'grep' | awk '{print $2}'`
  27. test "$pid" != "" && sudo kill $pid && echo "Stop apache-tomcat-80$i Successful!" || echo "Stop apache-tomcat-80$i Failed!"
  28. fi
  29. sleep 1
  30. done
  31. #echo
  32. }
  33. #启动
  34. start(){
  35. for((i=$BEGIN;i<=$END;i++))
  36. do
  37. #启动Tomcat
  38. #$PREFIX$i/bin/startup.sh && echo "Start tomcat7-$i Successful!"
  39. #echo
  40. if [ $i -lt 10 ]; then
  41. #echo "tomcat7-0$i"
  42. $PREFIX\0$i/bin/startup.sh && echo "Start apache-tomcat-800$i Successful!"
  43. else
  44. #echo "tomcat7-$i"
  45. $PREFIX$i/bin/startup.sh && echo "Start apache-tomcat-80$i Successful!"
  46. fi
  47. sleep 1
  48. done
  49. }
  50. status(){
  51. for((i=$BEGIN;i<=$END;i++))
  52. do
  53. #抽取Tomcat进程号PID
  54. #pid=`ps -ef | grep tomcat7-$i |grep -v 'grep' | awk '{print $2}'`
  55. #打印PID
  56. #echo "tomcat7-$i - $pid"
  57. if [ $i -lt 10 ]; then
  58. #echo "tomcat7-0$i"
  59. #$PREFIX\810$i/bin/startup.sh && echo "Start tomcat7-810$i Successful!"
  60. pid=`ps -ef | grep apache-tomcat-800$i/ |grep -v 'grep' | awk '{print $2}'`
  61. echo "apache-tomcat-800$i - $pid"
  62. else
  63. #echo "tomcat7-$i"
  64. #$PREFIX\81$i/bin/startup.sh && echo "Start tomcat7-81$i Successful!"
  65. pid=`ps -ef | grep apache-tomcat-80$i/ |grep -v 'grep' | awk '{print $2}'`
  66. echo "apache-tomcat-80$i - $pid"
  67. fi
  68. done
  69. }
  70. case "$1" in
  71. start)
  72. #echo "Start tomcat7-$BEGIN ~ tomcat7-$END"
  73. #echo
  74. start
  75. #echo
  76. ;;
  77. stop)
  78. #echo "Stop tomcat7-$BEGIN ~ tomcat7-$END"
  79. #echo
  80. stop
  81. #echo
  82. ;;
  83. restart)
  84. #echo "Restart tomcat7-$BEGIN ~ tomcat7-$END"
  85. #echo
  86. stop
  87. sleep 2
  88. start
  89. #echo
  90. ;;
  91. status)
  92. #echo "Pid of tomcat7-$BEGIN ~ tomcat7-$END"
  93. status
  94. #echo
  95. ;;
  96. *)
  97. echo $"Usage: $0 {start|stop|restart|status}"
  98. exit 1
  99. ;;
  100. esac





Nginx自检脚本(每60秒自动检测一次)

点击(此处)折叠或打开

  1. #!/bin/bash
  2. prefix=/usr/local/nginx_1.4.6/sbin
  3. while :
  4. do
  5.     nginxpid=`ps -C nginx --no-header | wc -l`
  6.     if [ $nginxpid -eq 0 ]; then
  7.     DATE=$(date +%Y-%m-%d %T)
  8.     killall nginx && $prefix/nginx && echo "$DATE Restart Nginx successful~" || echo "$DATE Fail to restart Nginx!"
  9.     sleep 60
  10.     else
  11.     DATE=$(date +%Y-%m-%d %T)
  12.     echo "$DATE : Nginx_Check is running, everything is OK~"
  13.     sleep 60
  14.     fi
  15. done

业务部署自动化脚本

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #脚本说明
  3. # 内容分发至各实例
  4. #变更记录
  5. #
  6. #系统环境变量
  7. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  8. export PATH
  9. #自定义变量
  10. bak=`date +%Y%m%d-%T`
  11. prefix=/usr/
  12. #tomcat_path=/usr/apache-tomcat-7.0.33/bin
  13. project=APP
  14. #begin=1
  15. #end=15
  16. #实例备份
  17. cd $prefix
  18. mkdir -p /home/user/$project/$bak
  19. \cp -rf $project /home/user/$project/$bak
  20. #根据实际情况修改,脚本参数如APP.zip解压后可以为APP的文件夹
  21. #也可以是APP文件夹下的内容
  22. cd /tmp/user/
  23. rm -rf ${project}
  24. mkdir $project
  25. cp $project\.war ./$project
  26. cd $project
  27. unzip ${project}\.war
  28. #unzip $1
  29. cd ..
  30. #chmod -R 755 $project
  31. #分发copy,如果所有的工程都指向一个文件夹,则无需分发,直接拷贝
  32. #for((i=$begin;i<=$end;i++))
  33. #do
  34. #根据需要增加覆盖copy的项目
  35. # \cp -rf $project $prefix-$i/webapps/
  36. #done
  37. #rm -rf $prefix$project
  38. cp -avpf $project\/* $prefix$project\/
  39. #Tomcat操作留给其他功能模块控制
  40. #cd $tomcat_path
  41. #./shutdown.sh
  42. #./startup.sh
  43. echo "update successful!"

懒人用删除Tomcat日志脚本(其实什么日志都能删)

点击(此处)折叠或打开

  1. #!/bin/bash
  2. #前缀路径
  3. PREFIX="/usr/TomcatSvr7-"
  4. #起始编号
  5. BEGIN=2
  6. #终止编号
  7. END=20
  8. for((i=$BEGIN;i<=$END;i++))
  9. do
  10.     #删除历史记录
  11.     rm -rf $prefix$i/logs/*0*
  12.     cat /dev/null > $prefix$i/logs/catalina.out
  13. done

Nginx日志管理

点击(此处)折叠或打开

  1. logs_path=\"/usr/local/nginx_1.4.6/logs/\"
  2. mv ${logs_path}www.log ${logs_path}www_$(date -d \"yesterday\" +\"%Y%m%d\").log
  3. mv ${logs_path}error.log ${logs_path}error_$(date -d \"yesterday\" +\"%Y%m%d\").log
  4. kill -HUP `cat ${logs_path}nginx.pid`
  5. find $logs_path -name \"www_*log\" -mtime +0 -exec rm -rf {} ;
  6. #find $logs_path -name \"access_*log\" -mtime +2 -exec rm -rf {} ;
  7. find $logs_path -name \"error_*log\" -mtime +0 -exec rm -rf {} ;

在root下输入crontab -e , 01 00 * * * /home/nginxlog.sh,指定每天0:01分自动执行脚本~

自动配置LVM分区

点击(此处)折叠或打开

  1. #!/bin/bash
  2. DEV=/dev/sdb
  3. VG=VG_DATA
  4. LG=lv_data
  5. DIR=/data
  6. pvcreate $DEV \
  7. && vgcreate $VG $DEV \
  8. && lvcreate -l 100%PVS -n $LG $VG \
  9. && mkfs.ext4 /dev/$VG/$LG \
  10. && mkdir $DIR
  11. && mount /dev/$VG/$LG $DIR


Nginx配置

点击(此处)折叠或打开

  1. server_tokens off;
  2. sendfile on;
  3. tcp_nopush on;
  4. tcp_nodelay on;
  5. keepalive_timeout 65;
  6. client_header_timeout 10;
  7. client_body_timeout 10;
  8. reset_timedout_connection on;
  9. send_timeout 10;
  10. charset utf-8;
  11. server_names_hash_bucket_size 128;
  12. client_header_buffer_size 512k;
  13. large_client_header_buffers 4 64k;
  14. client_max_body_size 16m;
  15. client_body_buffer_size 256k;
  16. proxy_connect_timeout 60;
  17. proxy_send_timeout 30;
  18. proxy_read_timeout 30;
  19. proxy_buffer_size 16k;
  20. proxy_buffers 4 256k;
  21. proxy_busy_buffers_size 512k;
  22. proxy_temp_file_write_size 512k;
  23. open_file_cache max=10240 inactive=20s;
  24. open_file_cache_valid 30s;
  25. open_file_cache_min_uses 10;
  26. open_file_cache_errors on


Tomcat连接池配置

点击(此处)折叠或打开

  1. port="5000"
  2. protocol="org.apache.coyote.http11.Http11NioProtocol"
  3. connectionTimeout="30000"
  4. URIEncoding="UTF-8"
  5. maxThreads="1000"
  6. minSpareThreads="100"
  7. acceptCount="300"
  8. acceptorThreadCount="6"
  9. keepAliveTimeout="120000"
  10. maxKeepAliveRequests="1000"
  11. enableLookups="false"
  12. redirectPort="8443" />

实时查看连接数

点击(此处)折叠或打开

  1. watch -n 1 "netstat -n | awk '/^tcp/ {++S[\$NF]} END {for(a in S) print a, S[a]}'"


远程切换Nginx

点击(此处)折叠或打开

  1. #!/bin/bash
  2. PORT1=8066
  3. PORT2=10066
  4. switch_to_8066(){
  5.     ssh 10.10.10.11 "/bin/bash /root/nginx_p$PORT1.sh;exit"
  6. }
  7. switch_to_10066(){
  8.     ssh 10.10.10.11 "/bin/bash /root/nginx_p$PORT2.sh;exit"
  9. }
  10. switch_to_all(){
  11.     ssh 10.10.10.11 "/bin/bash /root/nginx_all.sh;exit"
  12. }
  13. case "$1" in
  14. switch_to_8066)
  15. echo "switch nginx to port $PORT1"
  16. switch_to_8066
  17. echo "switch to port $PORT1 success"
  18. ;;
  19. switch_to_10066)
  20. echo "switch nginx to port $PORT2"
  21. switch_to_10066
  22. echo "switch to port $PORT2 success"
  23. ;;
  24. switch_to_all)
  25. echo "switch nginx to port $PORT1 and $PORT2"
  26. switch_to_all
  27. echo "switch to port $PORT2 and $PORT1 success"
  28. ;;
  29. *)
  30. echo $"Usage: $0 {switch_to_$PORT1|switch_to_$PORT2|switch_to_all}"
  31. exit 1
  32. ;;
  33. esac

被远程调用的脚本,只有红色部分有区别

点击(此处)折叠或打开

  1. #!/bin/bash
  2. NGINX_CONF=/usr/local/nginx_1.8.0/conf/
  3. NGINX_SBIN=/usr/local/nginx_1.8.0/sbin/nginx
  4. CONF_FILE=nginx_8066.conf
  5. ps -ef | grep nginx |grep -v 'grep' |grep -v 'nginx_p8066.sh' | awk '{print $2}' | xargs kill
  6. $NGINX_SBIN -c $NGINX_CONF$CONF_FILE



查看磁盘的TPS(每秒数据交换次数)

点击(此处)折叠或打开

  1. iostat -d sda | grep sda | awk '{ print $2; }'
  2. iostat -d | tail -n +4 | head -n -1 | awk '{s+=$2} END {print s}'


SVN自动发布(捡漏)

点击(此处)折叠或打开

  1. #!/bin/bash
  2. REMOTE_HOST1=192.168.1.100
  3. REMOTE_USER1=
  4. REMOTE_PWD1=
  5. DIR=TEST
  6. PROJECT=TEST_PRO
  7. BRANCH=trunk
  8. SVN_URL=http://192.168.1.233/svn/
  9. LOG=svn_file_change.log
  10. TMP_DIR=/home/test/
  11. TARGET_DIR=/data/save/
  12. if [ ! -d $TMP_DIR$DIR ]; then
  13.     mkdir $TMP_DIR$DIR
  14. else
  15.     echo "svn files is exist, script start......time : $(date -d today +%Y-%m-%d\ %H:%M:%S)" >> $TMP_DIR$LOG
  16.     #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
  17.     if [ $? -eq 0 ]; then
  18.         svn co $SVN_URL$PROJECT/$BRANCH/$DIR
  19.         if [ $? -eq 0 ]; then
  20.         expect -c "
  21.         spawn ssh $REMOTE_USER1@$REMOTE_HOST1 \"rm -rf $TARGET_DIR$DIR\"
  22.         expect {
  23.         \"*assword\" {set timeout 300; send \"$REMOTE_PWD1\r\";}
  24.         \"yes/no\" {send \"yes\r\"; exp_continue;}
  25.         }
  26.         expect eof"
  27.         expect -c "
  28.         spawn scp -r $TMP_DIR$DIR $REMOTE_USER1@$REMOTE_HOST1:$TARGET_DIR
  29.         expect {
  30.         \"*assword\" {set timeout 300; send \"$REMOTE_PWD1\r\";}
  31.         \"yes/no\" {send \"yes\r\"; exp_continue;}
  32.         }
  33.         expect eof"
  34.         expect -c "
  35.         spawn ssh $REMOTE_USER1@$REMOTE_HOST1 \"chown -R test:test $TARGET_DIR$DIR\"
  36.         expect {
  37.         \"*assword\" {set timeout 300; send \"$REMOTE_PWD1\r\";}
  38.         \"yes/no\" {send \"yes\r\"; exp_continue;}
  39.         }
  40.         expect eof"
  41.         #rm -rf $TMP_DIR$DIR
  42.         else
  43.             echo "svn error, can not copy the file!......" >> $TMP_DIR$LOG
  44.         fi
  45.     else
  46.         echo "Unkown error......" >> $TMP_DIR$LOG
  47.     fi
  48. fi


VIP

点击(此处)折叠或打开

  1. /sbin/ip addr add 100.100.100.100/24 dev eth0
  2. /sbin/ip addr del 100.100.100.100/24 dev eth0

posted on 2017-03-22 15:27  erisen  阅读(519)  评论(0编辑  收藏  举报

导航