Shell-day05
1. 昨日回顾
1. 流程控制语句之整数比较
-eq -ne -gt -lt -ge -le
2. 监测服务是否运行脚本
3. 条件测试,创建用户脚本
4. 函数库的使用,判断URL地址是否能够正常访问 /etc/init.d/functions
先引用 为真 action “” /bin/true 为假 action “” /bin/false
5. 流程控制语句之字符比较
== 精确匹配 != 不等于 -z 字符串为空则为真 -n 字符串不为空则为真
6. 多条件判断
-a 并且 -o 或者
必须使用双中括号 && ||
7. 判断学生成绩优劣的脚本
8. 流程控制语句之正则比较
=~ 必须使用双中括号
^ + $ [] {}
^[0-9]+$ ^[a-Z]+$
9. 创建用户脚本案例 要输入前缀和后缀
2. 清空日志,并保留最近的100行
1. 清空/var/log/messages
2. 要保留最近的100行内容
3. 只能是root超级管理员才能执行这个脚本
[root@shell /service/scripts/day05]
if [ "$USER" != "root" -o $UID -ne 0 ];then
echo "当前用户${USER}不是超级管理员用户!没有权限执行${0}脚本!"
exit
fi
File_Log=/var/log/messages
if [ -f $File_Log ];then
tail -100 $File_Log > ${File_Log}.bak && cat ${File_Log}.bak > $File_Log
if [ $? -eq 0 ];then
echo "${File_Log}日志清空成功!也保留了最近的100行内容!"
else
echo "${File_Log}日志清空失败!"
fi
else
echo "${File_Log}文件不存在!无法进行清空操作!"
fi
[root@shell /service/scripts/day05]
-rw------- 1 root root 56953 2020-04-24 09:20 /var/log/messages
[root@shell /service/scripts/day05]
838 /var/log/messages
[root@shell /service/scripts/day05]
[root@shell /service/scripts/day05]
/var/log/messages日志清空成功!也保留了最近的100行内容!
[root@shell /service/scripts/day05]
100 /var/log/messages
[root@shell /service/scripts/day05]
[root@shell /service/scripts/day05]
/var/log/messages文件不存在!无法进行清空操作!
[root@shell /service/scripts/day05]
[root@shell /service/scripts/day05]
[www@shell ~]$ sh /service/scripts/day05/if-1.sh
当前用户www不是超级管理员用户!没有权限执行/service/scripts/day05/if-1.sh脚本!
3. 判断服务是否正常运行
1. 服务是否启动 运行中 没有运行 没有安装 $1
判断位置变量是否为一个
判断服务是在什么状态 运行 no 没有安装
2. 判断服务端口是否存在 22 80 3306
netstat -lntp | grep nginx
3. 判断进程是否存在 ps aux
ps aux | grep nginx
[root@shell /service/scripts/day05]
if [ $# -ne 1 ];then
echo "Usage: $0 {sshd|rsync|nginx|php-fpm|mariadb|...}"
exit
fi
if [ $1 == "rsync" ];then
Server1=rsyncd
else
Server1=$1
fi
systemctl status $Server1 &>/dev/null
State=$?
if [ $State -eq 0 ];then
echo "${1}服务正在运行中..."
elif [ $State -eq 3 ];then
echo "${1}服务没有在运行..."
elif [ $State -eq 4 ];then
echo "系统没有安装${1}服务!"
exit
else
echo "当前系统中没有正在运行的进程ID号为:$1"
exit
fi
if [ $1 == "mariadb" ];then
Server=mysqld
else
Server=$1
fi
netstat -lntp | grep $Server &>/dev/null
if [ $? -eq 0 ];then
echo "${1}服务端口存在..."
else
echo "${1}服务端口不存在..."
fi
ps aux | grep $1 | grep -Ev 'grep|pts' &>/dev/null
if [ $? -eq 0 ];then
echo "${1}服务进程存在..."
else
echo "${1}服务进程不存在..."
fi
[root@shell /service/scripts/day05]
rsync服务正在运行中...
rsync服务端口存在...
rsync服务进程存在...
[root@shell /service/scripts/day05]
mariadb服务正在运行中...
mariadb服务端口存在...
mariadb服务进程存在...
[root@shell /service/scripts/day05]
sshd服务正在运行中...
sshd服务端口存在...
sshd服务进程存在...
[root@shell /service/scripts/day05]
系统没有安装nginx服务!
[root@shell /service/scripts/day05]
nfs服务没有在运行...
nfs服务端口不存在...
nfs服务进程不存在...
4. 根据不同的系统安装不同的源
根据你的操作系统 去安装不同的yum源
6 7 8
ll /etc/yum.repos.d/
1. 怎么知道当前系统为什么版本
[root@localhost ~]
CentOS release 6.4 (Final)
[root@shell /service/scripts/day05]
CentOS Linux release 7.6.1810 (Core)
[root@localhost tensorflow]
CentOS Linux release 8.0.1905 (Core)
2. 判断网络是否畅通
3. 移除旧的yum源
4. 安装系统yum源
5. 返回安装结果
[root@shell /service/scripts/day05]
Version=$(awk '{print $(NF-1)}' /etc/redhat-release)
Yum_Dir=/etc/yum.repos.d/
if [ ${Version%%.*} -eq 6 ];then
echo "当前操作系统为:${Version}系列!"
ping -c1 -W1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
echo "当前系统的网络是没有问题的!可以正常部署Yum源!"
else
echo "当期系统的网络不可达!不能进行部署Yum源!"
exit
fi
rm -f ${Yum_Dir}* && echo "旧的Yum仓库移除完毕!" || echo "旧的Yum仓库移除失败!"
echo "开始更新部署Yum源的Base源......"
curl -s -o ${Yum_Dir}CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
if [ $? -eq 0 ];then
echo "Yum源中的Base更新部署完毕!"
else
echo "Yum源中的Base更新部署失败!"
fi
echo "开始更新部署Yum源的Epel源......"
curl -s -o ${Yum_Dir}epel.repo http://mirrors.aliyun.com/repo/epel-6.repo &>/dev/null
if [ $? -eq 0 ];then
echo "Yum源中的Epel更新部署完毕!"
else
echo "Yum源中的Epel更新部署失败!"
fi
elif [ ${Version%%.*} -eq 7 ];then
echo "当前操作系统为:${Version}系列!"
ping -c1 -W1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
echo "当前系统的网络是没有问题的!可以正常部署Yum源!"
else
echo "当期系统的网络不可达!不能进行部署Yum源!"
exit
fi
rm -f ${Yum_Dir}* && echo "旧的Yum仓库移除完毕!" || echo "旧的Yum仓库移除失败!"
echo "开始更新部署Yum源的Base源......"
curl -s -o ${Yum_Dir}CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
if [ $? -eq 0 ];then
echo "Yum源中的Base更新部署完毕!"
else
echo "Yum源中的Base更新部署失败!"
fi
echo "开始更新部署Yum源的Epel源......"
curl -s -o ${Yum_Dir}epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &>/dev/null
if [ $? -eq 0 ];then
echo "Yum源中的Epel更新部署完毕!"
else
echo "Yum源中的Epel更新部署失败!"
fi
elif [ ${Version%%.*} -eq 8 ];then
echo "当前操作系统为:${Version}系列!"
ping -c1 -W1 www.baidu.com &>/dev/null
if [ $? -eq 0 ];then
echo "当前系统的网络是没有问题的!可以正常部署Yum源!"
else
echo "当期系统的网络不可达!不能进行部署Yum源!"
exit
fi
rm -f ${Yum_Dir}* && echo "旧的Yum仓库移除完毕!" || echo "旧的Yum仓库移除失败!"
echo "开始更新部署Yum源的Base源......"
curl -s -o ${Yum_Dir}CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo &>/dev/null
if [ $? -eq 0 ];then
echo "Yum源中的Base更新部署完毕!"
else
echo "Yum源中的Base更新部署失败!"
fi
echo "开始更新部署Yum源的Epel源......"
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm &>/dev/null && \
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' ${Yum_Dir}epel* &>/dev/null && \
sed -i 's|^metalink|#metalink|' ${Yum_Dir}epel* &>/dev/null
if [ $? -eq 0 ];then
echo "Yum源中的Epel更新部署完毕!"
else
echo "Yum源中的Epel更新部署失败!"
fi
else
echo "你的操作系统目前没有准备期Yum源!无法进行部署更新!"
fi
[root@shell /service/scripts/day05]
当前操作系统为:7.6.1810系列!
当前系统的网络是没有问题的!可以正常部署Yum源!
旧的Yum仓库移除完毕!
开始更新部署Yum源的Base源......
Yum源中的Base更新部署完毕!
开始更新部署Yum源的Epel源......
Yum源中的Epel更新部署完毕!
[root@shell /service/scripts/day05]
total 8
-rw-r--r-- 1 root root 2523 2020-04-24 11:11 CentOS-Base.repo
-rw-r--r-- 1 root root 664 2020-04-24 11:11 epel.repo
[root@shell /service/scripts/day05]
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
!base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,097
!epel/x86_64 Extra Packages for Enterprise Linux 7 - x86_64 13,249
!extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 341
!updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,787
repolist: 25,474
5. 流程控制语句之Case语句
case语句主要就是程序的选择、循环等进行控制
case $变量 in
Yes|Y|y)
命令
;;
变量2)
命令
;;
变量3)
命令
;;
变量n)
命令
;;
*)
无匹配的变量序列
esac
语法示例:
安装不同的Nginx版本
1、安装Nginx-1.16
2、安装Nginx-1.17
3、安装Nginx-1.18
4、退出脚本程序
[root@shell /service/scripts/day05]
cat<<EOF
####################
1、安装Nginx-1.16
2、安装Nginx-1.17
3、安装Nginx-1.18
4、退出脚本程序
####################
EOF
read -p "请输入你要安装的Nginx版本![1|2|3|4]:" Install
case $Install in
1)
echo "你选择了安装Nginx-1.16版本...."
echo "正在安装Nginx-1.16版本!请稍后...."
sleep 3
echo "Nginx-1.16版本安装成功...."
;;
2)
echo "你选择了安装Nginx-1.17版本...."
echo "正在安装Nginx-1.17版本!请稍后...."
sleep 3
echo "Nginx-1.17版本安装成功...."
;;
3)
echo "你选择了安装Nginx-1.18版本...."
echo "正在安装Nginx-1.18版本!请稍后...."
sleep 3
echo "Nginx-1.18版本安装成功...."
;;
4)
echo "你选择了退出脚本程序!正在退出...."
exit
;;
*)
echo "你的输入不符合要求!请输入[1|2|3|4]其中的数字编号!"
esac
[root@shell /service/scripts/day05]
1、安装Nginx-1.16
2、安装Nginx-1.17
3、安装Nginx-1.18
4、退出脚本程序
请输入你要安装的Nginx版本![1|2|3|4]:1
你选择了安装Nginx-1.16版本....
正在安装Nginx-1.16版本!请稍后....
Nginx-1.16版本安装成功....
6. 编写一个rsync启动脚本
1. 如何启动rsync
/usr/bin/rsync --daemon
2. 如何停止rsync
pkill rsync
3. 如何重启
先停止,后启动
4. 查看状态
/var/run/sshd.pid
当服务在运行的时候,pid文件是存在的
当服务不在运行时,pid文件是不存在的
[root@shell /service/scripts/day05]
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
if [ $# -ne 1 ];then
echo "Usage: $0 {start|stop|status|restart}"
exit
fi
Pid_File=/var/run/rsync.pid
case $1 in
start)
if [ -f $Pid_File ];then
action "服务Rsync正在运行中......" /bin/true
else
/usr/bin/rsync --daemon &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Rsync启动成功......" /bin/true
Pid=$(ps aux | grep rsync | awk 'NR==1{print $2}')
echo $Pid >$Pid_File
else
action "服务Rsync启动失败......" /bin/false
exit
fi
fi
;;
stop)
if [ -f $Pid_File ];then
pkill rsync &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Rsync停止成功......" /bin/true
rm -f $Pid_File &>/dev/null
else
action "服务Rsync停止失败......" /bin/false
fi
else
action "服务Rsync不在运行中......" /bin/true
fi
;;
status)
if [ -f $Pid_File ];then
action "服务Rsync正在运行中......" /bin/true
else
action "服务Rsync不在运行中......" /bin/true
fi
;;
restart)
if [ -f $Pid_File ];then
pkill rsync &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Rsync停止成功......" /bin/true
rm -f $Pid_File &>/dev/null
else
action "服务Rsync停止失败......" /bin/false
fi
/usr/bin/rsync --daemon &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Rsync启动成功......" /bin/true
Pid=$(ps aux | grep rsync | awk 'NR==1{print $2}')
echo $Pid >$Pid_File
else
action "服务Rsync启动失败......" /bin/false
exit
fi
else
action "服务Rsync不在运行中......" /bin/true
/usr/bin/rsync --daemon &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Rsync启动成功......" /bin/true
Pid=$(ps aux | grep rsync | awk 'NR==1{print $2}')
echo $Pid >$Pid_File
else
action "服务Rsync启动失败......" /bin/false
exit
fi
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
exit
esac
[root@shell /service/scripts/day05]
[root@shell /service/scripts/day05]
86 case-2.sh
[root@shell /service/scripts/day05]
Usage: case-2.sh {start|stop|status|restart}
[root@shell /service/scripts/day05]
服务Rsync不在运行中...... [ OK ]
[root@shell /service/scripts/day05]
服务Rsync启动成功...... [ OK ]
[root@shell /service/scripts/day05]
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 19892/rsync
tcp6 0 0 :::873 :::* LISTEN 19892/rsync
[root@shell /service/scripts/day05]
服务Rsync停止成功...... [ OK ]
[root@shell /service/scripts/day05]
[root@shell /service/scripts/day05]
服务Rsync不在运行中...... [ OK ]
[root@shell /service/scripts/day05]
服务Rsync不在运行中...... [ OK ]
服务Rsync启动成功...... [ OK ]
[root@shell /service/scripts/day05]
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 19926/rsync
tcp6 0 0 :::873 :::* LISTEN 19926/rsync
[root@shell /service/scripts/day05]
服务Rsync停止成功...... [ OK ]
服务Rsync启动成功...... [ OK ]
7. Nginx服务启动脚本
1. 如何启动Nginx
/usr/sbin/nginx
2. 如何停止
/usr/sbin/nginx -s stop
3. 如何重启restart
先停止,后启动
4. 平滑重启
/usr/sbin/nginx -s reload
在什么情况下才能进行平滑重启 只有nginx在运行的状态下,nginx才能进行平滑重启
/var/run/nginx.pid
[root@shell /service/scripts/day05]
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
if [ $# -ne 1 ];then
echo "Usage: $0 {start|stop|status|reload|restart}"
exit
fi
Pid_File=/var/run/nginx.pid
case $1 in
start)
if [ -f $Pid_File ];then
action "服务Nginx正在运行中...." /bin/true
else
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
fi
;;
stop)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx停止成功...." /bin/true
else
action "服务Nginx停止失败...." /bin/false
fi
else
action "服务Nginx不在运行中...." /bin/true
fi
;;
status)
if [ -f $Pid_File ];then
action "服务Nginx正在运行中...." /bin/true
else
action "服务Nginx不在运行中...." /bin/true
fi
;;
restart)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx停止成功...." /bin/true
else
action "服务Nginx停止失败...." /bin/false
fi
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
action "服务Nginx不在运行中...." /bin/true
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
fi
;;
reload)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s reload &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx平滑重启成功...." /bin/true
else
action "服务Nginx平滑重启失败...." /bin/false
fi
else
action "服务Nginx不在运行中!无法进行平滑重启操作...." /bin/false
fi
;;
*)
echo "Usage: $0 {start|stop|status|reload|restart}"
esac
[root@shell /service/scripts/day05]
[root@shell /service/scripts/day05]
Usage: case-3.sh {start|stop|status|reload|restart}
[root@shell /service/scripts/day05]
服务Nginx正在运行中.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx停止成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx不在运行中.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx不在运行中.... [ OK ]
服务Nginx启动成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx停止成功.... [ OK ]
服务Nginx启动成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx平滑重启成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx停止成功.... [ OK ]
[root@shell /service/scripts/day05]
服务Nginx不在运行中!无法进行平滑重启操作.... [FAILED]
[root@shell /service/scripts/day05]
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
Suo=/tmp/nginx.lock
if [ -f $Suo ];then
echo "此脚本${0}正在运行中!请稍后再执行...."
exit
fi
if [ $# -ne 1 ];then
echo "Usage: $0 {start|stop|status|reload|restart}"
exit
fi
touch $Suo
Pid_File=/var/run/nginx.pid
case $1 in
start)
if [ -f $Pid_File ];then
action "服务Nginx正在运行中...." /bin/true
else
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
fi
;;
stop)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx停止成功...." /bin/true
else
action "服务Nginx停止失败...." /bin/false
fi
else
action "服务Nginx不在运行中...." /bin/true
fi
;;
status)
if [ -f $Pid_File ];then
action "服务Nginx正在运行中...." /bin/true
else
action "服务Nginx不在运行中...." /bin/true
fi
;;
restart)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx停止成功...." /bin/true
else
action "服务Nginx停止失败...." /bin/false
fi
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
action "服务Nginx不在运行中...." /bin/true
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
fi
;;
reload)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s reload &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx平滑重启成功...." /bin/true
else
action "服务Nginx平滑重启失败...." /bin/false
fi
else
action "服务Nginx不在运行中!无法进行平滑重启操作...." /bin/false
fi
;;
*)
echo "Usage: $0 {start|stop|status|reload|restart}"
esac
rm -f $Suo
[root@shell /service/scripts/day05]
此脚本case-3.sh正在运行中!请稍后再执行....
[root@shell /service/scripts/day05]
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
Suo=/tmp/nginx.lock
if [ -f $Suo ];then
echo "此脚本${0}正在运行中!请稍后再执行...."
exit
fi
if [ $# -ne 1 ];then
echo "Usage: $0 {start|stop|status|reload|restart}"
exit
fi
touch $Suo
Pid_File=/var/run/nginx.pid
case $1 in
start)
if [ -f $Pid_File ];then
action "服务Nginx正在运行中...." /bin/true
else
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
Error=/tmp/nginx_error.log
/usr/sbin/nginx -t &>$Error
Nginx_File=$(awk -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
Nginx_Line=$(awk -F '[ :]' 'NR==1{print $NF}' $Error)
/usr/sbin/nginx -t
read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
case $Confirm in
Yes|y|Y)
vim +$Nginx_Line $Nginx_File
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
echo "语法检查还是失败!你自己手动修改去吧!"
fi
;;
No|N|n)
echo "你选择了不修改配置文件!你可以手动修改!"
;;
*)
echo "你输入不符合要求!请重新输入!"
esac
fi
fi
;;
stop)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx停止成功...." /bin/true
else
action "服务Nginx停止失败...." /bin/false
fi
else
action "服务Nginx不在运行中...." /bin/true
fi
;;
status)
if [ -f $Pid_File ];then
action "服务Nginx正在运行中...." /bin/true
else
action "服务Nginx不在运行中...." /bin/true
fi
;;
restart)
if [ -f $Pid_File ];then
/usr/sbin/nginx -s stop &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx停止成功...." /bin/true
else
action "服务Nginx停止失败...." /bin/false
fi
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
Error=/tmp/nginx_error.log
/usr/sbin/nginx -t &>$Error
Nginx_File=$(awk -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
Nginx_Line=$(awk -F '[ :]' 'NR==1{print $NF}' $Error)
/usr/sbin/nginx -t
read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
case $Confirm in
Yes|y|Y)
vim +$Nginx_Line $Nginx_File
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx -s stop &>/dev/null && sleep 2
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
echo "语法检查还是失败!你自己手动修改去吧!"
fi
;;
No|N|n)
echo "你选择了不修改配置文件!你可以手动修改!"
;;
*)
echo "你输入不符合要求!请重新输入!"
esac::
fi
else
action "服务Nginx不在运行中...." /bin/true
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
Error=/tmp/nginx_error.log
/usr/sbin/nginx -t &>$Error
Nginx_File=$(awk -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
Nginx_Line=$(awk -F '[ :]' 'NR==1{print $NF}' $Error)
/usr/sbin/nginx -t
read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
case $Confirm in
Yes|y|Y)
vim +$Nginx_Line $Nginx_File
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx启动成功...." /bin/true
else
action "服务Nginx启动失败...." /bin/false
fi
else
echo "语法检查还是失败!你自己手动修改去吧!"
fi
;;
No|N|n)
echo "你选择了不修改配置文件!你可以手动修改!"
;;
*)
echo "你输入不符合要求!请重新输入!"
esac
fi
fi
;;
reload)
if [ -f $Pid_File ];then
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx -s reload &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx平滑重启成功...." /bin/true
else
action "服务Nginx平滑重启失败...." /bin/false
fi
else
Error=/tmp/nginx_error.log
/usr/sbin/nginx -t &>$Error
Nginx_File=$(awk -F '[ :]' 'NR==1{print $(NF-1)}' $Error)
Nginx_Line=$(awk -F '[ :]' 'NR==1{print $NF}' $Error)
/usr/sbin/nginx -t
read -p "Nginx配置文件语法检查失败!错误的配置文件为:${Nginx_File}错误的行为第${Nginx_Line}行:是否进行配置修改[Yes|No]:" Confirm
case $Confirm in
Yes|y|Y)
vim +$Nginx_Line $Nginx_File
/usr/sbin/nginx -t &>/dev/null
if [ $? -eq 0 ];then
echo "Nginx语法检查成功...."
/usr/sbin/nginx -s reload &>/dev/null && sleep 2
if [ $? -eq 0 ];then
action "服务Nginx平滑重启成功...." /bin/true
else
action "服务Nginx平滑重启失败...." /bin/false
fi
else
echo "语法检查还是失败!你自己手动修改去吧!"
fi
;;
No|N|n)
echo "你选择了不修改配置文件!你可以手动修改!"
;;
*)
echo "你输入不符合要求!请重新输入!"
esac
fi
else
action "服务Nginx不在运行中!无法进行平滑重启操作...." /bin/false
fi
;;
*)
echo "Usage: $0 {start|stop|status|reload|restart}"
esac
rm -f $Suo
[root@shell /service/scripts/day05]
服务Nginx停止失败.... [FAILED]
nginx: [emerg] unknown directive "id" in /etc/nginx/nginx.conf:7
nginx: configuration file /etc/nginx/nginx.conf test failed
Nginx配置文件语法检查失败!错误的配置文件为:/etc/nginx/nginx.conf错误的行为第7行:是否进行配置修改[Yes|No]:y
Nginx语法检查成功....
服务Nginx启动成功.... [ OK ]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现