运行级别脚本2
上一篇《運(yùn)行級(jí)別腳本》主要是從概念上認(rèn)知運(yùn)行級(jí)別腳本。本篇主要記錄一些實(shí)用性的知識(shí)。
一、運(yùn)行級(jí)別腳本的結(jié)構(gòu)
[root@localhost rc0.d]# cat /etc/init.d/ntpd #!/bin/bash # # ntpd This shell script takes care of starting and stopping # ntpd (NTPv4 daemon). # #下面這句中的-表示默認(rèn)不啟動(dòng),58和74分別表示系統(tǒng)啟動(dòng)和退出時(shí)啟動(dòng)、關(guān)閉服務(wù)的優(yōu)先級(jí) # chkconfig: - 58 74 #以下為運(yùn)行級(jí)別腳本的描述信息 # description: ntpd is the NTPv4 daemon. \ # The Network Time Protocol (NTP) is used to synchronize the time of \ # a computer client or server to another server or reference time source, \ # such as a radio or satellite receiver or modem.#省略部分為讀取配置文件相關(guān)內(nèi)容 ......#定義啟動(dòng)函數(shù)start start() {# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 1readconf;if [ -n "$dostep" ]; thenecho -n $"$prog: Synchronizing with time server: "/usr/sbin/ntpdate $dropstr -s -b $NTPDATE_OPTIONS $tickers &>/dev/nullRETVAL=$?[ $RETVAL -eq 0 ] && success || failureechoif [ $RETVAL -eq 0 ]; then[ "$SYNC_HWCLOCK" = "yes" ] && sync_hwclockelseOPTIONS="$OPTIONS -g"fielse# -g can replace the grep for time servers# as it permits ntpd to violate its 1000s limit once.OPTIONS="$OPTIONS -g"fi# Start daemons.echo -n $"Starting $prog: "daemon ntpd $OPTIONSRETVAL=$?echo[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ntpdreturn $RETVAL }#定義停止服務(wù)函數(shù)stop stop() {echo -n $"Shutting down $prog: "killproc ntpdRETVAL=$?echo[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ntpdreturn $RETVAL }#下面是判斷參數(shù)1的結(jié)構(gòu) # See how we were called. case "$1" instart)start;;stop)stop;;status)status ntpdRETVAL=$?;;restart|reload)stopstartRETVAL=$?;;condrestart)if [ -f /var/lock/subsys/ntpd ]; thenstopstartRETVAL=$?fi;; #如果參數(shù)1不是以上參數(shù),則輸出提示信息*)echo $"Usage: $0 {start|stop|restart|condrestart|status}"RETVAL=3 esac#設(shè)置退出狀態(tài)并退出 exit $RETVAL這是一個(gè)非常長的腳本,上面僅截取了該腳本的一部分,此部分正好說明了運(yùn)行級(jí)別腳本的編寫方式。
(1)首先需要以注釋的方式聲明服務(wù)默認(rèn)啟動(dòng)的運(yùn)行級(jí)別列表,系統(tǒng)啟動(dòng)、關(guān)閉時(shí)服務(wù)啟動(dòng)、關(guān)閉的優(yōu)先級(jí)。
(2)通常將服務(wù)的啟動(dòng)、關(guān)閉操作都寫在函數(shù)中,然后以函數(shù)的方式調(diào)用。
(3)使用case語句處理strart、stop等參數(shù)。
(4)在case語句最后處理不合適的參數(shù)。
(5)如果需要檢查服務(wù)是否處于運(yùn)行狀態(tài),最好創(chuàng)建運(yùn)行標(biāo)記文件。
注意:設(shè)置運(yùn)行級(jí)別腳本啟動(dòng)、關(guān)閉的優(yōu)先級(jí)時(shí),一定要注意其依賴的其他服務(wù)的優(yōu)先級(jí),以免啟動(dòng)服務(wù)失敗。
二、編寫運(yùn)行級(jí)別腳本
編寫運(yùn)行級(jí)別腳本的目的可能是為自己編寫的監(jiān)控腳本添加新的啟動(dòng)方式,也可能是為某個(gè)產(chǎn)品編寫啟動(dòng)服務(wù)等。此處引入一個(gè)示例腳本(并無實(shí)質(zhì)性內(nèi)容,在實(shí)際編寫時(shí)需要在函數(shù)中添加實(shí)際的啟動(dòng)、關(guān)閉服務(wù)的語句):
[root@localhost shell]# cat test #!/bin/bash#定義服務(wù)啟動(dòng)的運(yùn)行級(jí)別為3、4、5,啟動(dòng)和關(guān)閉的優(yōu)先級(jí)分別為80、10 # chkconfig:345 80 10 #以下這句是服務(wù)的描述信息 # description:This is a test service#This is a test script #2013.12.20#定義參數(shù)錯(cuò)誤的提示消息函數(shù)usage function usage() {echo "Usage:$0{start|stop|restart|reload}"return 0 }#定義啟動(dòng)服務(wù)的函數(shù)start function start() {echo "Starting $0:"return 0 }#定義關(guān)閉服務(wù)的函數(shù)stop function stop() {echo "Shutting down $0:"return 0 }#腳本的主體部分 #使用case語句判斷參數(shù)的值 case $1 instart)start;;stop)stop;;restart|reload)stopstart;;*)usageexit 1 esac exit 0注意:“# chkconfig”和“# description”開頭的這兩行,這是運(yùn)行級(jí)別腳本中必須定義的內(nèi)容,主要用于添加服務(wù)時(shí)初始化配置和描述信息。
三、添加和管理運(yùn)行級(jí)別腳本
編寫運(yùn)行級(jí)別腳本的目的是能夠?qū)⒛_本作為服務(wù)添加到系統(tǒng)中。
1、添加運(yùn)行級(jí)別腳本為系統(tǒng)服務(wù)
將運(yùn)行級(jí)別腳本添加為系統(tǒng)服務(wù)之前,應(yīng)該將編寫完成的運(yùn)行級(jí)別腳本復(fù)制到目錄/etc/init.d中,并添加相應(yīng)的執(zhí)行權(quán)限。完成之后,就可以使用chkconfig命令將運(yùn)行級(jí)別腳本添加為系統(tǒng)服務(wù)了。
將示例腳本test添加為系統(tǒng)服務(wù)使用如下命令:
[root@localhost shell]# chkconfig --add test添加成功后,命令不會(huì)有任何返回信息??梢允褂靡韵旅畈榭刺砑拥椒?wù):
[root@localhost shell]# chkconfig --list test test 0:off 1:off 2:off 3:on 4:on 5:on 6:off [root@localhost shell]# chkconfig --list | grep test test 0:off 1:off 2:off 3:on 4:on 5:on 6:off另外,使用chkconfig –add test命令添加test為系統(tǒng)服務(wù)成功后,我們可以發(fā)現(xiàn)各個(gè)運(yùn)行級(jí)別目錄里已經(jīng)自動(dòng)添加了test的相關(guān)啟動(dòng)、關(guān)閉的腳本,并且這些腳本都是/etc/init.d/test的鏈接文件:
[root@localhost shell]# ls -al /etc/rc.d/rc0.d | grep test lrwxrwxrwx 1 root root 14 Dec 20 20:08 K10test -> ../init.d/test [root@localhost shell]# ls -al /etc/rc.d/rc1.d | grep test lrwxrwxrwx 1 root root 14 Dec 20 20:08 K10test -> ../init.d/test [root@localhost shell]# ls -al /etc/rc.d/rc2.d | grep test lrwxrwxrwx 1 root root 14 Dec 20 20:08 K10test -> ../init.d/test [root@localhost shell]# ls -al /etc/rc.d/rc3.d | grep test lrwxrwxrwx 1 root root 14 Dec 20 20:08 S80test -> ../init.d/test [root@localhost shell]# ls -al /etc/rc.d/rc4.d | grep test lrwxrwxrwx 1 root root 14 Dec 20 20:08 S80test -> ../init.d/test [root@localhost shell]# ls -al /etc/rc.d/rc5.d | grep test lrwxrwxrwx 1 root root 14 Dec 20 20:08 S80test -> ../init.d/test [root@localhost shell]# ls -al /etc/rc.d/rc6.d | grep test lrwxrwxrwx 1 root root 14 Dec 20 20:08 K10test -> ../init.d/test2、管理使用運(yùn)行級(jí)別腳本添加到服務(wù)
使用chkconfig命令添加服務(wù)之后,就可以使用命令service啟動(dòng)服務(wù):
[root@localhost shell]# service test start Starting /etc/init.d/test:當(dāng)然也可以隨時(shí)刪除:
[root@localhost shell]# chkconfig --del test轉(zhuǎn)載于:https://www.cnblogs.com/nufangrensheng/p/3484443.html
總結(jié)
- 上一篇: 绝对路径 相对路径
- 下一篇: 创客运动引发第三次工业革命