shell脚本之nginx启动脚本、统计日志字段、for循环实战、跳板机
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循环实战、跳板机的更多相关文章
- 【shell脚本】nginx启动脚本
[root@localhost init.d]# cat nginx #!/bin/bash #nx Startup script for the Nginx HTTP Server # it ver ...
- 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/{ ...
- linux nginx 启动脚本
linux nginx 启动脚本 [root@webtest76 ~]# vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the ...
- Nginx 启动脚本/重启脚本
第一步先运行命令关闭nginx sudo kill `cat /usr/local/nginx/logs/nginx.pid` 第二步 vi /etc/init.d/nginx 输入以下内容 #!/b ...
- nginx启动脚本,手动编辑
nginx启动脚本,手动编辑 #! /bin/bash # chkconfig: - # description: nginx service XDIR=/www/server/nginx DESC= ...
- Nginx 启动脚本
Nginx 启动脚本 1.vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # S ...
- LNMP 1.4 nginx启动脚本和配置文件
编写Nginx启动脚本,写入下面这段,授权755 vim /etc/init.d/nginx #!/bin/bash # chkconfig: - # description: http servic ...
- nginx启动脚本和配置文件
1.编写Nginx启动脚本,并加入系统服务 vim /etc/init.d/nginx并在其中写入如下内容:#!/bin/bash# chkconfig: - 30 21# description: ...
- nginx启动脚本(class练习)
说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys import os from subprocess import Popen,PIPE ...
随机推荐
- Zabbix 4.0.2试用(七):在Linux主机中安装zabbix agent并添加该主机(yum源安装)
Zabbix 4.0.2试用(七):在Linux主机中安装zabbix agent并添加主机(yum源安装) 2018年12月20日, 上午6:42 之前介绍的是下载源安装包,编译安装的方式来安装ag ...
- python-线性回归预测
导入包 # Required Packages import matplotlib.pyplot as plt import numpy as np import pandas as pd from ...
- -webkit-scrollbar 的使用,滚动条的隐藏
滚动条的隐藏 -webkit-scrollbar 是一个伪类选择器 设置滚动条的样式 例如滚动条的隐藏 元素::-webkit-scrollbar{ width:0; }
- RBAC 权限模型
RBAC 0 模型 最基本的 MySQL 脚本,没有建立外键约束. /* Navicat Premium Data Transfer Source Server Type : MySQL Source ...
- 【makefile】make程序的命令行选项和参数
Make命令参数的典型序列如下所示: make [-f makefile文件名][选项][宏定义][目标] 这里用[]括起来的表示是可选的.命令行选项由破折号“–”指明,后面跟选项,如: make – ...
- GitHub:Python
ylbtech-GitHub:Python 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://github.com/python 2. 6 ...
- Android和jS互调技术Demo实现
package com.loaderman.webviewdemo; import android.os.Bundle; import android.support.v7.app.AppCompat ...
- 吐血整理,一文读懂中国金融衍生品市场的前世今生和未来[z]
[z]https://www.gfedu.cn/cfrm/content_22687.shtml 从定义上来说,金融衍生工具(derivativesecurity)是在货币.债券.股票等传统金融工具的 ...
- python programming作业11 Qt designer (打地鼠,不是很完美)
不导包的代码 from PyQt5 import QtCore, QtGui, QtWidgets import sys from PyQt5.QtWidgets import QApplicati ...
- 2019.11.29 SAP SMTP郵件服務器配置 發送端 QQ郵箱
今天群裏的小夥伴問了如何配置郵件的問題,隨自己在sap裏面配置了一個 1. RZ10配置參數 a) 参数配置前,先导入激活版本 执行完毕后返回 b) 输入参数文件DEFAU ...