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

一、源码安装nginx

[root@localhost Desktop]# tar zxf nginx-1.17.8.tar.gz 
[root@localhost Desktop]# ls
nginx-1.17.8  nginx-1.17.8.tar.gz

解决依赖性:

yum install -y gcc openssl-devel pcre-devel

安装:

cd nginx-1.17.8/
./configure --prefix=/usr/local/nginx
make && make install

起服务

/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -V #查看版本信息
netstat -ntulp | grep nginx

二、nginx服务启动脚本的编写:

/etc/init.d/编写脚本nginxd

[root@localhost init.d]# cat nginxd
#!/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
		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"
	fi
}

function stop() {
        if [ `netstat -antlpe|grep nginx|wc -l` -ne 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 not running"
		return 0
	fi
}

case "$1" in
	start)
	start
	;;
	stop)
	stop
	;;
	restart)
	stop
	sleep 1
	start
	;;
	*)
		echo $"USeage: $0 {start|stop|restart}"
		exit 1
esac

测试:

cd /etc/init.d/
[root@localhost init.d]# sh nginxd start
nginx is started                                           [  OK  ]
[root@localhost init.d]# sh nginxd stop
nginx is stoped                                            [  OK  ]
[root@localhost init.d]# sh nginxd start
nginx is started                                           [  OK  ]
[root@localhost init.d]# sh nginxd restart
nginx is stoped                                            [  OK  ]
nginx is started                                           [  OK  ]
[root@localhost init.d]# sh nginxd restartttt
USeage: nginxd {start|stop|restart}

你可能感兴趣的:(Shell,linux,nginx,shell,运维)