shell—服务启动脚本的编写
1.安装nginx
首先在官网获得nginx的安装包
tar zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
yum install gcc zlib zlib-devel openssl-devel -y #解决依赖性
./configure --prefix=/usr/local/nginx
make && make install
当看到Makefile和objs时说明安装成功
2.启动和关闭nginx
/usr/local/nginx/sbin/nginx #启动
/usr/local/nginx/sbin/nginx -s stop #关闭
netstat -antlpe | grep nginx |wc -l #查看端口状态(0表示关闭,1表示开启)
3.服务启动脚本的编写
vim /etc/init.d/nginxd
脚本内容:
#!/bin/bash
. /etc/init.d/functions #加载系统函数库
path=/usr/local/nginx/sbin #设定nginx启动命令路径
function start() {
if [ `netstat -antlpe | grep nginx | wc -l` -eq 0 ] #如果等于0
then
$path/nginx #打开nginx
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
action "nginx is started" /bin/true
return $RETVAL
else
action "nginx is started" /bin/false
return $RETVAL
fi
else
echo "nginx is running"
return 0
fi
}
function stop() {
if [ `netstat -antlpe | grep nginx | wc -l` -ne 0 ] #如果不等于0
then
$path/nginx -s stop
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
action "nginx is stoped" /bin/true
return $RETVAL
else
action "nginx is stoped" /bin/false
return $RETVAL
fi
else
echo "nginx is stoped"
return 0
fi
}
case "$1" in #通过特殊参数$1接收脚本参数的字符串
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
*)
echo $"usage: $0 {start | stop | restart}"
exit 1
esac
exit $RETVAL
给它加上执行权限:
chmod +x /etc/init.d/nginxd
测试: