shell 服务启动脚本

shell 服务启动脚本

安装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

  
  
  • 1
  • 2
  • 3
  • 4
  • 5

启动和关闭nginx

/usr/local/nginx/sbin/nginx 			#启动
/usr/local/nginx/sbin/nginx -s stop		#关闭
netstat -antlpe | grep nginx |wc -l		#查看端口状态(0表示关闭,1表示开启)

  
  
  • 1
  • 2
  • 3

服务启动脚本的编写
脚本内容:

#!/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

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

给它加上执行权限:
chmod +x /etc/init.d/nginxd
然后即可利用如下的命令

/usr/local/nginx/sbin/nginx 			#启动
/usr/local/nginx/sbin/nginx -s stop		#关闭
netstat -antlpe | grep nginx |wc -l		#查看端口状态(0表示关闭,1表示开启)

  
  
  • 1
  • 2
  • 3
发布了107 篇原创文章 · 获赞 0 · 访问量 1450

猜你喜欢

转载自blog.csdn.net/weixin_45029822/article/details/104365539