Shell开发rsync启动脚本
2018-10-14 20:06
543 查看
需求:实现shell脚本对rsync的start|stop|restart
rsync pid所在路径:/var/run/rsyncd.pid
rsync启动命令:rsync --daemon
rsync进程停止命令:pkill rsync
文中通过判断rsync pid所在路径和进程状态来实现rsync的start|stop|restart
1)脚本演示
[root@jason scripts]# cat rsync.sh #!/bin/sh #This is the launch script for rsync. #------------------------------------- #Author:jason #QQ:760966297 #mile:jasonminghao@163.com #------------------------------------- [ -f /etc/init.d/functions ] && . /etc/init.d/functions || exit 1 PID_PATH=/var/run/rsyncd.pid fun_usage(){ echo "Usage:$0 {start|stop|restart}" exit 2 } fun_start(){ if [ ! -e $PID_PATH ]; then rsync --daemon PORT=`netstat -lntup |grep rsync|wc -l` [ $PORT -eq 2 ]&& action "Rsync is startup successful." /bin/true ||action "Rsync start failed." /bin/false; else action "Rsync is running." /bin/false fi } fun_stop(){ if [ ! -e $PID_PATH ];then action "Rsync is not run." /bin/false else while true do pkill rsync sleep 2 PORT=`netstat -lntup |grep rsync|wc -l` [ $PORT -eq 0 ]&&action "Rsync is stop successful." /bin/true && break done fi } fun_restart(){ if [ ! -e $PID_PATH ];then action "Rsync is not run." /bin/false exit 2 else fun_stop >/dev/null sleep 2 fun_start >/dev/null PORT=`netstat -lntup |grep rsync|wc -l` [ $PORT -eq 2 ]&&action "Rsync is startup successful." /bin/true || action "Rsync is restart failed." /bin/false fi } case $1 in start) fun_start ;; stop) fun_stop ;; restart) fun_restart ;; *) fun_usage ;; esac
2)脚本测试
#测试fun_usage函数 [root@jason scripts]# sh rsync.sh #<==运行脚本,脚本后面没有加参数的提示 Usage:rsync.sh {start|stop|restart} #测试start [root@jason scripts]# sh rsync.sh start #<==启动rsync Rsync is startup successful. [ OK ] [root@jason scripts]# netstat -lntup |grep 873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 98926/rsync tcp 0 0 :::873 :::* LISTEN 98926/rsync #测试stop [root@jason scripts]# sh rsync.sh stop #<==关闭rsync ^[[ARsync is stop successful. [ OK ] [root@jason scripts]# netstat -lntup |grep 873 #测试restart [root@jason scripts]# sh rsync.sh restart #<==重启rsync,但是rsync进程未开启,提示报错 Rsync is not run. b60 [FAILED] [root@jason scripts]# sh rsync.sh start #<==先启动rsync Rsync is startup successful. [ OK ] [root@jason scripts]# sh rsync.sh restart #<==然后再测试restart Rsync is startup successful. [ OK ]
相关文章推荐
- 企业Shell面试题15:开发Rsync服务启动脚本案例
- 利用shell开发rsync服务启动脚本
- 开发rsync服务的启动脚本
- storm集群统一启动和停止shell脚本开发
- 开发启动rsync服务的脚本
- 通过Shell开发企业级专业服务启动脚本案例(MySQL)
- shell开发的启动和停止应用的脚本
- 企业级通过shell脚本开发MySQL启动脚本 案例
- rsync启动关闭shell脚本
- linux系统服务rsync启动脚本
- SHELL编写redis启动脚本
- linux使用shell脚本管理其他服务器Tomcat启动停止、自动发布
- shell编写redis启动脚本
- Linux 启动停止SpringBoot jar 程序部署Shell 脚本
- shell脚本开发的6点基本规范和优秀习惯
- android系统开发小知识-启动脚本文件内部的执行顺序 .
- Shell脚本应用之服务启动脚本
- linux环境下编写shell脚本实现启动停止tomcat服务的方法
- linux中shell脚本启动报错
- shell脚本检查服务是否启动脚本