日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

运行级别脚本2

發布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 运行级别脚本2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上一篇《運行級別腳本》主要是從概念上認知運行級別腳本。本篇主要記錄一些實用性的知識。

一、運行級別腳本的結構

[root@localhost rc0.d]# cat /etc/init.d/ntpd #!/bin/bash # # ntpd This shell script takes care of starting and stopping # ntpd (NTPv4 daemon). # #下面這句中的-表示默認不啟動,58和74分別表示系統啟動和退出時啟動、關閉服務的優先級 # chkconfig: - 58 74 #以下為運行級別腳本的描述信息 # 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.#省略部分為讀取配置文件相關內容 ......#定義啟動函數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 }#定義停止服務函數stop stop() {echo -n $"Shutting down $prog: "killproc ntpdRETVAL=$?echo[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ntpdreturn $RETVAL }#下面是判斷參數1的結構 # 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;; #如果參數1不是以上參數,則輸出提示信息*)echo $"Usage: $0 {start|stop|restart|condrestart|status}"RETVAL=3 esac#設置退出狀態并退出 exit $RETVAL

這是一個非常長的腳本,上面僅截取了該腳本的一部分,此部分正好說明了運行級別腳本的編寫方式。

(1)首先需要以注釋的方式聲明服務默認啟動的運行級別列表,系統啟動、關閉時服務啟動、關閉的優先級。

(2)通常將服務的啟動、關閉操作都寫在函數中,然后以函數的方式調用。

(3)使用case語句處理strart、stop等參數。

(4)在case語句最后處理不合適的參數。

(5)如果需要檢查服務是否處于運行狀態,最好創建運行標記文件。

注意:設置運行級別腳本啟動、關閉的優先級時,一定要注意其依賴的其他服務的優先級,以免啟動服務失敗。

二、編寫運行級別腳本

編寫運行級別腳本的目的可能是為自己編寫的監控腳本添加新的啟動方式,也可能是為某個產品編寫啟動服務等。此處引入一個示例腳本(并無實質性內容,在實際編寫時需要在函數中添加實際的啟動、關閉服務的語句):

[root@localhost shell]# cat test #!/bin/bash#定義服務啟動的運行級別為3、45,啟動和關閉的優先級分別為80、10 # chkconfig:345 80 10 #以下這句是服務的描述信息 # description:This is a test service#This is a test script #2013.12.20#定義參數錯誤的提示消息函數usage function usage() {echo "Usage:$0{start|stop|restart|reload}"return 0 }#定義啟動服務的函數start function start() {echo "Starting $0:"return 0 }#定義關閉服務的函數stop function stop() {echo "Shutting down $0:"return 0 }#腳本的主體部分 #使用case語句判斷參數的值 case $1 instart)start;;stop)stop;;restart|reload)stopstart;;*)usageexit 1 esac exit 0

注意:“# chkconfig”和“# description”開頭的這兩行,這是運行級別腳本中必須定義的內容,主要用于添加服務時初始化配置和描述信息。

三、添加和管理運行級別腳本

編寫運行級別腳本的目的是能夠將腳本作為服務添加到系統中。

1、添加運行級別腳本為系統服務

將運行級別腳本添加為系統服務之前,應該將編寫完成的運行級別腳本復制到目錄/etc/init.d中,并添加相應的執行權限。完成之后,就可以使用chkconfig命令將運行級別腳本添加為系統服務了。

將示例腳本test添加為系統服務使用如下命令:

[root@localhost shell]# chkconfig --add test

添加成功后,命令不會有任何返回信息。可以使用以下命令查看添加到服務:

[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為系統服務成功后,我們可以發現各個運行級別目錄里已經自動添加了test的相關啟動、關閉的腳本,并且這些腳本都是/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/test

2、管理使用運行級別腳本添加到服務

使用chkconfig命令添加服務之后,就可以使用命令service啟動服務:

[root@localhost shell]# service test start Starting /etc/init.d/test:

當然也可以隨時刪除:

[root@localhost shell]# chkconfig --del test

轉載于:https://www.cnblogs.com/nufangrensheng/p/3484443.html

總結

以上是生活随笔為你收集整理的运行级别脚本2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。