您好,登錄后才能下訂單哦!
首先在寫(xiě)腳本之前,你要知道關(guān)于rsync的理論知識(shí)與實(shí)操,才能寫(xiě)好一個(gè)ok的腳本。
我們先來(lái)捋一捋:
rsync的啟動(dòng)命令:
rsync --daemon
停止命令:
kill rsync進(jìn)程號(hào)
pkill rsync
killall rsync
話不多說(shuō),上腳本:
#!/bin/bash ##################################### #?File?Name:?rsyncd.sh #?Version:?V1.0 #?Author:?wuhan #?Created?Time?:?2018-10-14?13:19:53 #?Description:?rsync?service?script ##################################### .?/etc/init.d/functions #首先加載/etc/init.d/functions,待會(huì)我們需要用里面的基礎(chǔ)函數(shù) lockfile="/var/lock/subsys/rsyncd" #定義一個(gè)鎖文件的變量 rpfp="/var/run/rsyncd.pid" #寫(xiě)一個(gè)rsync端口ID的變量,方便多次引用,可在此腳本中使用 #寫(xiě)一個(gè)函數(shù),名為start start(){ ????#函數(shù)名 ????rsync?--daemon?&&?>/dev/null ????#rsync服務(wù)啟動(dòng)命令,啟動(dòng)成功后輸出到空 ????retval=$? ????#獲取返回值 ????if?[?$retval?-eq?0?] ????#如果返回值等于0 ????then ????#那么執(zhí)行如下操作 ????????action?"rsync?startup?ok"?/bin/true ????????#用action函數(shù)輸出啟動(dòng)成功的內(nèi)容并顯示ok ????????touch?$lockfile ????????#在創(chuàng)建一個(gè)鎖文件 ????????return?$retval ????????#返回使用啟動(dòng)命令后的返回值 ????else ????#否則 ????????action?"rsync?startup?fail"?/bin/false ????????#輸出服務(wù)啟動(dòng)失敗的內(nèi)為,顯示false ????????return?$retval ????????#也返回啟動(dòng)命令后的返回值,方便排錯(cuò) ????fi ????#退出 } #寫(xiě)一個(gè)停止命令的函數(shù),名為stop stop(){ ????if?test?-s?"$rpfp" ????#如果端口ID文件長(zhǎng)度不為0,那么為真,執(zhí)行如下操作 ????then ????????rsyncd_pid=`cat?$rpfp` ????????#定義一個(gè)端口ID號(hào)的變量 ????????if?(kill?-0?$rsyncd_pid?&>/dev/null) ????????#如果端口ID號(hào)存在,且輸出到空 ????????then ????????#執(zhí)行如下操作 ????????????kill?$rsyncd_pid ????????????#通過(guò)進(jìn)程號(hào)殺掉rsync服務(wù) ????????????retval=$? ????????????#獲取返回值 ????????????if?[?$retval?-eq?0?] ????????????#如果返回值等于0,執(zhí)行如下指令 ????????????then ????????????????action?"rsync?stop?ok"?/bin/true ????????????????#輸出服務(wù)停止成功,顯示ok ????????????????rm?-fr?$lockfile ????????????????#如果殺掉rsync服務(wù),那就刪除鎖文件 ????????????????return?"$retval" ????????????????#獲取返回值 ????????????else ????????????#否則執(zhí)行如下操作 ????????????????action?"rsync?stop?fail"?/bin/false ????????????????#輸出rsync停止失敗,顯示false ????????????????return?$retval ????????????????#獲得返回值 ????????????fi ????????else ????????#否則 ????????????echo?"rsyncd?process?is?not?exist." ????????????#提示rsync進(jìn)程不存在 ????????????return?2 ????????????#返回值2 ????????fi ????else ????#否則 ????????echo?"${rpfp}is?not?exist,or?rsync?does?not?startup" ????????#提示端口號(hào)不存在或rsync服務(wù)沒(méi)啟動(dòng) ????fi ????#結(jié)束 } case?"$1"?in #因?yàn)閷?xiě)了函數(shù),所以我們這里用case結(jié)構(gòu)條件句更適合,定義$1為變量 ????start) ????#匹配到start?執(zhí)行start函數(shù)并獲取返回值 ????????start ????????retval=$? ????????;; ????stop) ????#匹配到stop?執(zhí)行stop函數(shù)并獲取返回值 ????????stop ????????retval=$? ????????;; ????restart) ????#匹配到restart?先執(zhí)行stop函數(shù)休眠1s在執(zhí)行start實(shí)現(xiàn)restart,并獲取返回值 ????????stop ????????sleep?1 ????????start ????????retval=$? ????????;; ????*) ????#都沒(méi)有匹配到就輸出提示只能執(zhí)行三個(gè)函數(shù)中的一個(gè) ????????echo?"?usage:?$0?{start|stop|restart}?" ????????exit?1 esac #結(jié)束 exit?$retval #退出并取得返回值從而進(jìn)行判斷
試驗(yàn):聰明的你可以看到PID進(jìn)程號(hào)在我們的停止開(kāi)啟和重啟之后更改,說(shuō)明腳本ok。
[root@node1?~]#?lsof?-i:873 COMMAND???PID?USER???FD???TYPE?DEVICE?SIZE/OFF?NODE?NAME rsync???10044?root????4u??IPv4?639767??????0t0??TCP?*:rsync?(LISTEN) rsync???10044?root????5u??IPv6?639768??????0t0??TCP?*:rsync?(LISTEN) [root@node1?~]#?sh?/server/scripts/09/rsyncd3.sh?stop rsync?stop?ok??????????????????????????????????????????????[??OK??] [root@node1?~]#?lsof?-i:873 [root@node1?~]#? [root@node1?~]#?sh?/server/scripts/09/rsyncd3.sh?start rsync?startup?ok???????????????????????????????????????????[??OK??] [root@node1?~]#?lsof?-i:873 COMMAND???PID?USER???FD???TYPE?DEVICE?SIZE/OFF?NODE?NAME rsync???10212?root????4u??IPv4?664268??????0t0??TCP?*:rsync?(LISTEN) rsync???10212?root????5u??IPv6?664269??????0t0??TCP?*:rsync?(LISTEN) [root@node1?~]#?sh?/server/scripts/09/rsyncd3.sh?restart rsync?stop?ok??????????????????????????????????????????????[??OK??] rsync?startup?ok???????????????????????????????????????????[??OK??] [root@node1?~]#?lsof?-i:873 COMMAND???PID?USER???FD???TYPE?DEVICE?SIZE/OFF?NODE?NAME rsync???10227?root????4u??IPv4?664325??????0t0??TCP?*:rsync?(LISTEN) rsync???10227?root????5u??IPv6?664326??????0t0??TCP?*:rsync?(LISTEN)
此腳本直接復(fù)制粘貼即可使用,需要/etc/rsyncd.conf配置好pid和鎖文件
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。