1.NGINX启动脚本

  1. #!/bin/bash
  2. # chkconfig: 235 32 62
  3. # description: nginx
  4. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  5. pidfile=/application/nginx/logs/nginx.pid
  6. start(){
  7. if [ -f $pidfile ];then
  8. echo "Nginx is Running"
  9. else
  10. /application/nginx/sbin/nginx
  11. RETVAL=$?
  12. if [ $RETVAL -eq 0 ];then
  13. action "Nginx is Started" /bin/true
  14. else
  15. action "Nginx is Started" /bin/false
  16. fi
  17. fi
  18. return $RETVAL
  19. }
  20. stop(){
  21. if [ -f $pidfile ];then
  22. /application/nginx/sbin/nginx -s stop
  23. RETVAL=$?
  24. if [ $RETVAL -eq 0 ];then
  25. action "Nginx is Stopped" /bin/true
  26. else
  27. action "Nginx is Stopped" /bin/false
  28. fi
  29. else
  30. echo "Nginx is not Running"
  31. fi
  32. return $RETVAL
  33. }
  34. reload(){
  35. if [ -f $pidfile ];then
  36. /application/nginx/sbin/nginx -s reload &>/dev/null
  37. RETVAL=$?
  38. if [ $RETVAL -eq 0 ];then
  39. action "Nginx is Reloaded" /bin/true
  40. else
  41. action "Nginx is Reloaded" /bin/false
  42. fi
  43. else
  44. echo "Nginx is not Running"
  45. /application/nginx/sbin/nginx
  46. RETVAL=$?
  47. if [ $RETVAL -eq 0 ];then
  48. action "Nginx is Started" /bin/true
  49. else
  50. action "Nginx is Started" /bin/false
  51. fi
  52. fi
  53. return $RETVAL
  54. }
  55. # case中的RETVAL=$?是为了接收函数的返回值
  56. case $1 in
  57. start)
  58. start
  59. RETVAL=$?
  60. ;;
  61. stop)
  62. stop
  63. RETVAL=$?
  64. ;;
  65. restart)
  66. stop
  67. sleep 1
  68. start
  69. RETVAL=$?
  70. ;;
  71. reload)
  72. reload
  73. RETVAL=$?
  74. ;;
  75. *)
  76. echo "USAGE: $0 {start|stop|restart|reload} "
  77. exit 1
  78. esac
  79. exit $RETVAL

添加到系统服务中

  1. cp /server/scripts/nginx /etc/init.d/
  2. chkconfig --add nginx
  3. # 生成了这些文件
  4. /etc/rc.d/rc2.d/S32nginx /etc/rc.d/rc3.d/S32nginx
  5. /etc/rc.d/rc4.d/S32nginx /etc/rc.d/rc5.d/S32nginx
  6. /etc/rc.d/rc0.d/K62nginx /etc/rc.d/rc1.d/K62nginx
  7. /etc/rc.d/rc6.d/K62nginx
  8.  
  9. 0-6表示不同运行级别:
  10. 0:关机
  11. 1:单用户
  12. 2:无网络的多用户
  13. 3:命令模式
  14. 4:未启用
  15. 5:图形界面模式
  16. 6:重启
  17. # 查看运行级别
  18. who -r
  19. runlevel
  20. 235表示不管是这三个运行级别中的哪一个,nginx的启动顺序都是第32
  21. 06表示重启或关机时,nginx的关闭顺序是第62

2.统计日志文件流量字段之和

  1. #!/bin/bash
  2. exec < access_2018-12-8.log
  3. while read line
  4. do
  5. i=`echo $line|awk '{print $10}'`
  6. expr $i + 1 &>/dev/null
  7. if [ $? -ne 0 ];then
  8. continue
  9. fi
  10. ((sum+=i))
  11. done
  12. [ -n "$sum" ] && echo $sum
  13.  
  14. #!/bin/bash
  15. while read line
  16. do
  17. i=`echo $line|awk '{print $10}'`
  18. expr $i + 1 &>/dev/null
  19. if [ $? -ne 0 ];then
  20. continue
  21. fi
  22. ((sum+=i))
  23. done< access_2018-12-8.log
  24. 或者cat a.log | while read line
  25. [ -n "$sum" ] && echo $sum

用shell取苹果

  1. #!/bin/bash
  2. file="/root/iplist"
  3. exec < $file
  4. while read line
  5. do
  6. echo $line >> /root/ReverseIp
  7. done
  8. mv $file $file.bak
  9. cat /root/ReverseIp | tac

3.for循环实战,创建十个文件,名字为随机的八个字符

  1. #!/bin/bash
  2. mkdir test1
  3. cd ./test1
  4. for((i=1;i<=10;i++))
  5. do
  6. touch `echo $RANDOM |md5sum | cut -c 1-8`.html
  7. done

批量修改文件名中指定的字符串

  1. file="192916b7_finished.html"
  2. mv $file `echo $file | sed 's#_finished.html#.jpg#g'`
  3. # 第二种方法
  4. ls test1 | awk -F '[_]' '{print "mv " $0,$1".jpg"}'
  5. # 第三种方法
  6. rename "_finished.html" ".jpg" /root/test1/*.html
  7. # 第四种方法,sed后向引用
  8. mv $file `echo $file | sed -r 's#(^.*)_finished.html#\1.jpg#g'`

4.跳板机

知识点1:trap信号;

知识点2:ssh key免密钥登录;

ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa  > /dev/null 2>&1

知识点3:/etc/profile.d/:登录系统时会加载该目录,放在/etc/profile.d/的文件,即使没有x权限也能被执行.

想要cat的EOF不顶格写,第二个EOF前面是TAB键,不是四个空格.

  1. cat /server/scripts/tiaoban.sh
  2. #!/bin/bash
  3. trapper(){
  4. trap "" HUP INT QUIT TERM TSTP
  5. }
  6. menu(){
  7. cat <<-EOF
  8. ========Host List================
  9. 1)172.16.1.8
  10. 2)172.16.1.31
  11. 3)172.16.1.41
  12. 4)exit
  13. ==================================
  14. EOF
  15. }
  16.  
  17. conn_host(){
  18. case "$1" in
  19. 1)
  20. ssh $USER@172.16.1.8
  21. ;;
  22. 2)
  23. ssh $USER@172.16.1.31
  24. ;;
  25. 3)
  26. ssh $USER@172.16.1.41
  27. ;;
  28. 4)
  29. exit
  30. ;;
  31. *)
  32. continue
  33. esac
  34. }
  35. main(){
  36. while true
  37. do
  38. clear
  39. menu
  40. read -p "Pls select:" num
  41. conn_host $num
  42. done
  43. }
  44.  
  45. trapper
  46. main
  47.  
  48. cat /etc/profile.d/tiaobanfirst.sh
  49. #!/bin/bash
  50. [ $UID -ne 0 ] && [ $USER != "oldgirl" ] &&\
  51. . /server/scripts/tiaoban.sh

如果这个脚本中不写第一行,则永远也登不上这台机器了,只能虚拟机恢复快照或物理机重装系统.

b.安全方面

1)跳板机禁止外网IP登录,只能内网IP登录;

2)其他服务器也禁止外网IP登录,同时禁止root登录,做完ssh认证,将密码登录也禁止,只允许密钥登录,并且只有跳板机的密钥放在其他服务器上;

PasswordAuthentication yes改为no

3)通过VPN连到跳板机,再从跳板机登录到其他服务器.

从零开始搭建创业公司后台技术栈:http://www.phppan.com/2018/04/svr-stack/

shell脚本之nginx启动脚本、统计日志字段、for循环实战、跳板机的更多相关文章

  1. 【shell脚本】nginx启动脚本

    [root@localhost init.d]# cat nginx #!/bin/bash #nx Startup script for the Nginx HTTP Server # it ver ...

  2. centos LNMP第一部分环境搭建 LAMP LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/php/{p.conf.default,p.conf} php运行方式SAPI介绍 第二十三节课

    centos  LNMP第一部分环境搭建 LAMP安装先后顺序  LNMP安装先后顺序 php安装 安装nginx  编写nginx启动脚本   懒汉模式  mv   /usr/local/php/{ ...

  3. linux nginx 启动脚本

    linux nginx 启动脚本 [root@webtest76 ~]# vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the ...

  4. Nginx 启动脚本/重启脚本

    第一步先运行命令关闭nginx sudo kill `cat /usr/local/nginx/logs/nginx.pid` 第二步 vi /etc/init.d/nginx 输入以下内容 #!/b ...

  5. nginx启动脚本,手动编辑

    nginx启动脚本,手动编辑 #! /bin/bash # chkconfig: - # description: nginx service XDIR=/www/server/nginx DESC= ...

  6. Nginx 启动脚本

    Nginx 启动脚本 1.vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # S ...

  7. LNMP 1.4 nginx启动脚本和配置文件

    编写Nginx启动脚本,写入下面这段,授权755 vim /etc/init.d/nginx #!/bin/bash # chkconfig: - # description: http servic ...

  8. nginx启动脚本和配置文件

    1.编写Nginx启动脚本,并加入系统服务 vim /etc/init.d/nginx并在其中写入如下内容:#!/bin/bash# chkconfig: - 30 21# description: ...

  9. nginx启动脚本(class练习)

    说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys import os from subprocess import Popen,PIPE ...

随机推荐

  1. c++ 派生类的构造函数 与 基类构造函数的关系

    <面向对象程序设计基础(第二版>李师贤等,第254页:C++语言的基本规则是:创建一个派生类的对象时,如果基类带有构造函数,则先调用基类的构造函数,然后才调用派生类的构造函数. <T ...

  2. TCP发送窗口更新tcp_ack_update_window

    在tcp_ack接收ACK处理函数中,如果确认当前走慢速路径,那么会调用tcp_ack_update_window函数检查窗口是否需要更新并更新之,并且更新未确认数据的位置,即更新窗口左边沿: sta ...

  3. Java-JDK-windows和linux版-百度云下载

    链接: https://pan.baidu.com/s/15vjk4PNzuItd5vHJ6deq3Q 关注以下公众号,回复[9757],获取提取码 linux:jdk-8u221-linux-x64 ...

  4. SRS之SrsConfig类

    1. 类定义 1.1 SrsConfig 类 /** * the config service provider. * for the config supports reload, so never ...

  5. 凸包Graham Scan算法实现

    凸包算法实现点集合中搜索凸包顶点的功能,可以处理共线情况,可以输出共线点也可以不输出而只输出凸包顶点.经典的Graham Scan算法,点排序使用极角排序方式,并对共线情况做特殊处理.一般算法是将共线 ...

  6. JSP学习案例--,竞猜游戏

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  7. c#阿里云短信验证码

    发送验证码 private static void SendAcs(string mobile, string templateCode, dynamic json, int ourid) { if ...

  8. Python 中的type和object详解

    1.python中的类 Python2.x 中的类分为两种,一种是所有继承自object的新式类,另外一种是经典类classobj, 新式类的写法: class A(object): pass 经典类 ...

  9. pyqt5在QMainWindow中布局的问题

    在pyqt5中使用了父类为QMainWindow的话,在里面使用布局类,QGridLayout, QHBoxLayout ,QVBoxLayout 时,发现不好用. 解决: 如果是在以QWidget为 ...

  10. redis的日常操作(1)

    一.简介 [概述] redis是一种nosql数据库,他的数据是保存在内存中,同时redis可以定时把内存数据同步到磁盘,即可以将数据持久化,并且他比memcached支持更多的数据结构(string ...