shell之服务启动脚本的编写(以ngix和rsycn为例)

一、ngix的服务启动脚本的编写

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

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

4.测试:

sh /etc/init.d/nginxd start

二、rsync的启动脚本

1.首先,查看软件包是否安装
 rpm -qa rsync rsync-3.0.6-9.el6_4.1.x86_64
2.我们创建一个新的文件
 touch /etc/rsyncd.conf
3.启动rsync
rsync --daemon
netstat -antlpe | grep 873
    
4.停止rsync
 pkill rsync
 netstat -antlpe | grep 873

5.开发脚本
cat /etc/init.d/rsyncd 
#!/bin/bash
. /etc/init.d/functions function usage() {
    echo $"usage:$0 {start|stop|restart}"
    exit 1
}  
function start() {     rsync --daemon
    sleep 1
    if [ `netstat -antlpe|grep rsync|wc -l` -ge 1 ];then action "rsyncd is started." /bin/true     else actiom "rsyncd id started." /bin/false
    fi
 
}  
function stop() {     killall rsync &>/dev/null
    sleep 2
    if [ `netstat -antlpe|grep rsync|wc -l` -eq 0 ];then action "rsyncd is stopped." /bin/true     else action "rsyncd is stopped." /bin/false
    fi
 
}  
function main(){   if [ $# -ne 1 ];then usage $0
     fi      case "$1" in start)     start
    ;; stop)     stop     ;;         restart)     stop
            start             ;;
*)     usage $0
            ;;       esac
 
}  main $*

测试

 sh /etc/init.d/rsyncd stop
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值