shell编程----服务启动脚本的编写


1. nginx的安装和使用

nginx的安装

tar zxf nginx-1.17.8.tar.gz 
cd nginx-1.17.8/
yum install gcc openssl-devel pcre-devel zlib -y
./configure --prefix=/usr/local/nginx
make && make install
# 安装成功后,可以在/usr/local/nginx下看到安装好的文件
[root@localhost nginx]# pwd
/usr/local/nginx
[root@localhost nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp

nginx的启动和关闭

/usr/local/nginx/sbin/nginx 			#启动
/usr/local/nginx/sbin/nginx -s stop		#关闭
netstat -antlpe | grep nginx |wc -l		#查看端口状态(0表示关闭,1表示开启)
[root@localhost nginx-1.17.8]# netstat -antlupe|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          29041      4811/nginx: master  

2. nginx服务开启和停止脚本

#!/bin/bash

. /etc/init.d/functions # 加载系统函数可以
path=/usr/local/nginx/sbin # 设定nginx启动命令路径

function start(){
	if [ `netstat -antlpe|grep nginx|wc -l` -eq 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 $RETVAL
	fi
} 

function stop(){
	if [ `netstat -antlupe|grep nginx|wc -l` -ne 0 ] 
	then
		$path/nginx -s stop
		RETVAL=$?
		if [ $REVAL -eq 0 ]
		then
			action "nginx is stoped" /bin/true
		else
			action "nginx is toped" /bin/false
		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 

测试:

[root@localhost init.d]# /etc/init.d/nginxd start
Reloading systemd:                                         [  OK  ]
Starting nginxd (via systemctl):                           [  OK  ]
[root@localhost init.d]# /etc/init.d/nginxd stop
Stopping nginxd (via systemctl):                           [  OK  ]
[root@localhost init.d]# /etc/init.d/nginxd restart
Restarting nginxd (via systemctl):                         [  OK  ]
[root@localhost init.d]# /etc/init.d/nginxd haha
usage: /etc/init.d/nginxd{start|stop|restart}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值