1.NGINX启动脚本

#!/bin/bash
# chkconfig: 235 32 62
# description: nginx
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
pidfile=/application/nginx/logs/nginx.pid
start(){
if [ -f $pidfile ];then
echo "Nginx is Running"
else
/application/nginx/sbin/nginx
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Nginx is Started" /bin/true
else
action "Nginx is Started" /bin/false
fi
fi
return $RETVAL
}
stop(){
if [ -f $pidfile ];then
/application/nginx/sbin/nginx -s stop
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Nginx is Stopped" /bin/true
else
action "Nginx is Stopped" /bin/false
fi
else
echo "Nginx is not Running"
fi
return $RETVAL
}
reload(){
if [ -f $pidfile ];then
/application/nginx/sbin/nginx -s reload &>/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Nginx is Reloaded" /bin/true
else
action "Nginx is Reloaded" /bin/false
fi
else
echo "Nginx is not Running"
/application/nginx/sbin/nginx
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Nginx is Started" /bin/true
else
action "Nginx is Started" /bin/false
fi
fi
return $RETVAL
}
# case中的RETVAL=$?是为了接收函数的返回值
case $1 in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart)
stop
sleep 1
start
RETVAL=$?
;;
reload)
reload
RETVAL=$?
;;
*)
echo "USAGE: $0 {start|stop|restart|reload} "
exit 1
esac
exit $RETVAL

添加到系统服务中

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

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

#!/bin/bash
exec < access_2018-12-8.log
while read line
do
i=`echo $line|awk '{print $10}'`
expr $i + 1 &>/dev/null
if [ $? -ne 0 ];then
continue
fi
((sum+=i))
done
[ -n "$sum" ] && echo $sum #!/bin/bash
while read line
do
i=`echo $line|awk '{print $10}'`
expr $i + 1 &>/dev/null
if [ $? -ne 0 ];then
continue
fi
((sum+=i))
done< access_2018-12-8.log
或者cat a.log | while read line
[ -n "$sum" ] && echo $sum

用shell取苹果

#!/bin/bash
file="/root/iplist"
exec < $file
while read line
do
echo $line >> /root/ReverseIp
done
mv $file $file.bak
cat /root/ReverseIp | tac

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

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

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

file="192916b7_finished.html"
mv $file `echo $file | sed 's#_finished.html#.jpg#g'`
# 第二种方法
ls test1 | awk -F '[_]' '{print "mv " $0,$1".jpg"}'
# 第三种方法
rename "_finished.html" ".jpg" /root/test1/*.html
# 第四种方法,sed后向引用
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键,不是四个空格.

cat /server/scripts/tiaoban.sh
#!/bin/bash
trapper(){
trap "" HUP INT QUIT TERM TSTP
}
menu(){
cat <<-EOF
========Host List================
1)172.16.1.8
2)172.16.1.31
3)172.16.1.41
4)exit
==================================
EOF
} conn_host(){
case "$1" in
1)
ssh $USER@172.16.1.8
;;
2)
ssh $USER@172.16.1.31
;;
3)
ssh $USER@172.16.1.41
;;
4)
exit
;;
*)
continue
esac
}
main(){
while true
do
clear
menu
read -p "Pls select:" num
conn_host $num
done
} trapper
main cat /etc/profile.d/tiaobanfirst.sh
#!/bin/bash
[ $UID -ne 0 ] && [ $USER != "oldgirl" ] &&\
. /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. FRP

    使用 FRP 反向代理实现 Windows 远程连接 互联网普及率的日渐攀升与 IPv4 资源的持续减少,现在大部分家庭宽带都不会分配公网 IP ,这使一些网络应用的实现多了些困难,像个人的 NAS ...

  2. oracle 中怎样实现分页和去处重复

    oracle 中用关键字 rownum 来进行分页 rownum  不能使用大于号,只能是使用小于号,可以使用子查询和rownum一起使用来创建分页 SELECT * FROM ( SELECT e. ...

  3. IDEA:Process finished with exit code -1073741819 (0xC0000005)

    出门左转:https://www.cnblogs.com/virgosnail/p/10335224.html

  4. SRS之RTMP的TCP线程(即监听线程)

    本文分析的是 SRS 针对 rtmp 的端口建立的 tcp 线程.具体建立过程: SRS之监听端口的管理:RTMP RTMP 的 TCP 线程中各个类之间 handler 的关系图 1. RTMP之T ...

  5. 【python / mxnet / gluoncv / jupyter notebook】基于mxnet和gluoncv的图像分割

    程序环境为高性能集群: CPU:Intel Xeon Gold 6140 Processor * 2(共36核心)内存:512GB RAMGPU:Tesla P100-PCIE-16GB * 2 In ...

  6. github搜索不到代码的问题

    Hi team, Please check the following three query url :https://github.com/Konctantin/GreyMagic/search? ...

  7. 转载 筛子算法之golang实现求素数解析

    package main import "fmt" // Send the sequence 2, 3, 4, ... to channel 'ch'. func generate ...

  8. 使用Statement执行DML和DQL语句

    import com.loaderman.util.JdbcUtil; import java.sql.Connection; import java.sql.DriverManager; impor ...

  9. javascript循环语句

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 集成学习之Adaboost算法原理

    在boosting系列算法中,Adaboost是最著名的算法之一.Adaboost既可以用作分类,也可以用作回归. 1. boosting算法基本原理 集成学习原理中,boosting系列算法的思想: