函数的定义
# 方式一
start () {
echo "nginx start .... [OK]"
#return 0
}
stop () {
echo "nginx stop .... [FAIL]"
}
# 方式二
function start {
echo "nginx start .... [OK]"
}
# 函数的调用
start
stop
案例-编译安装nginx启动jio本
#!/usr/bin/env bash
nginx_install_doc=/usr/local/nginx
nginxd=${nginx_install_doc}/sbin/nginx
pid_file=${nginx_install_doc}/logs/nginx.pid
if [ -f $pid_file ]; then
nginx_pid=`cat ${pid_file}`
nginx_num=`ps -ef | grep ${nginx_pid} | grep -v "grep" | wc -l`
fi
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
else
echo "not found file /etc/init.d/functions"
exit
fi
start() {
if [ -f ${pid_file} ] && [ ${nginx_num} -ge 1 ]; then
echo "nginx running..."
else
if [ -f $pid_file ] && [ $nginx_num -lt 1 ]; then
rm -rf $pid_file
echo " nginx start `daemon $nginxd` "
fi
action "nginx start" $nginxd
fi
}
stop() {
if [ -f $pid_file ] && [ $nginx_num -ge 1 ]; then
action "nginx stop" pkill nginx
else
action "nginx stop" pkill nginx &> /dev/null
fi
}
restart() {
stop
sleep 1
start
}
status() {
if [ -f $pid_file ] && [ $nginx_num -ge 1 ]; then
echo "nginx running"
else
echo "nginx stop"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "错误输入,请重新执行"
;;
esac
#!/usr/bin/env bash
nginx_install_doc=/usr/local/nginx #定义nginx安装目录变量
nginxd=${nginx_install_doc}/sbin/nginx #定义nginx启动文件变量
pid_file=${nginx_install_doc}/logs/nginx.pid #定义nginx的pid文件变量
if [ -f $pid_file ]; then #如果这个pid文件存在
nginx_pid=`cat ${pid_file}` #查看pid并赋值
nginx_num=`ps -ef | grep ${nginx_pid} | grep -v "grep" | wc -l` #nginx进程数
fi
if [ -f /etc/init.d/functions ]; then #调用内部函数
. /etc/init.d/functions
else
echo "not found file /etc/init.d/functions"
exit
fi
start() {
if [ -f ${pid_file} ] && [ ${nginx_num} -ge 1 ]; then #如果pid文件存在,进程数大于等于1
echo "nginx running..." #说明nginx启动着
else
if [ -f $pid_file ] && [ $nginx_num -lt 1 ]; then #如果pid文件存在,之前意外关闭
rm -rf $pid_file #删除pid文件
echo " nginx start `daemon $nginxd` " #启动
fi
action "nginx start" $nginxd #其他情况,直接正常启动
fi
}
stop() {
if [ -f $pid_file ] && [ $nginx_num -ge 1 ]; then #如果pid文件存在,进程数大于等于1
action "nginx stop" pkill nginx #正常关闭
else
action "nginx stop" pkill nginx &> /dev/null #如果关闭着nginx,报错
fi
}
restart() {
stop
sleep 1
start
}
status() {
if [ -f $pid_file ] && [ $nginx_num -ge 1 ]; then
echo "nginx running"
else
echo "nginx stop"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "错误输入,请重新执行"
;;
esac
baim0手撸版
#!/bin/bash
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
else
echo "not found file /etc/init.d/functions"
exit
fi
print_action() {
if [ $2 -eq 0 ]; then
action "$1" /bin/true
else
action "$1" /bin/false
fi
}
start() {
nginx -t
if [ $? -eq 0 ]; then
print_action "nginx配置文件正确" 0
systemctl start nginx
if [ $? -eq 0 ]; then
print_action "Nginx 启动成功" 0
else
print_action "启动失败,请检查端口是否被占用"
fi
else
print_action "nginx配置文件错误,请检查" 1
fi
}
stop() {
systemctl stop nginx
if [ $? -eq 0 ]; then
print_action "Nginx 停止成功" 0
else
print_action "Nginx 停止失败" 1
fi
}
restart() {
stop
sleep 1
start
}
status() {
systemctl status nginx
if [ $? -eq 0 ]; then
print_action "正在查看状态" 0
else
print_action "Nginx未启动" 1
fi
}
rpm -ql nginx >/dev/null
if [ $? -ne 0 ]; then
PS3="Nginx尚未安装,是否安装>>:"
select item in {"安装","不安装"}; do
case $item in
安装)
cat >/etc/yum.repos.d/nginx.repo <<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
yum clean all
yum makecache
yum install nginx -y
print_action "Nginx安装成功,请重启脚本" 0
exit
;;
不安装)
exit
;;
esac
done
else
PS3="请输入对nginx的操作编号>>:"
select item in start stop status restart; do
case $item in
"start")
start
;;
"stop")
stop
;;
"restart")
restart
;;
"status")
status
;;
*)
print_action "请准确输入操作选项" 1
;;
esac
done
fi