標籤:服務指令碼
概述:CentOS 5和6下的服務啟動時,使用的命令都是service 服務名 start方式啟動,其實service命令也是調用了/etc/init.d/下的指令碼,下面請看具體步驟
實驗步驟:
首先準備好服務指令碼,下面這一段代碼是我之前寫的,其類似於我們的服務啟動時接受的參數,然後根據你所輸入的參數來返回輸出的內容
[[email protected] ~]# vim testsrv #!/bin/bash#function: script test#author: xiaoshui#[ -z $1 ] && echo "empty" &&exitdir=`basename $0`Dir="/var/lock/subsys/$dir"var=`echo $1 | tr ‘A-Z‘ ‘a-z‘`case $var instart) if [ ! -e "$Dir" ];then touch $Dir && echo "$dir start success" else echo "$dir already start" fi ;;stop) if [ ! -e "$Dir" ];then echo "$dir is not start" else rm -f $Dir && echo "$dir stop success" fi ;;restart) if [ ! -e "$Dir" ];then echo "$dir not start" touch $Dir && echo "$dir restart success" else rm $Dir && echo "$dir stopped" touch $Dir && echo "$dir restart success" fi ;;status) if [ ! -e "$Dir" ];then echo "$dir is stopped" else echo "$dir is running" fi ;;*) echo "argues error,please input start|restart|stop|status" ;;esac
第一步:將指令碼拷貝至/etc/init.d/目錄下,並在前面的#!/bin/bash下加上一行#chkconfig:35 12 88
[[email protected] ~]# vim /etc/init.d/testsrv #!/bin/bash#function: script test#chkconfig:35 12 88#description: test service#author: xiaoshui
加上這內容的意義在於,35表示初始在哪個層級下啟動,-表示預設都不啟動 12 88 表示其表示/etc/rc.d/rc#.d/下面的K和S大頭的檔案,其含義就是啟動的優先順序。
第二步:chkconfig --add 服務 將服務加到chkconfig列表中
[[email protected] rc.d]# chkconfig --add testsrv[[email protected] rc.d]# chkconfig --list ......省略........testsrv 0:off 1:off 2:off 3:on 4:off 5:on 6:off.......省略.....
第三步:使用service命令進行測試
[[email protected] rc.d]# service testsrv starttestsrv start success[[email protected] rc.d]# service testsrv stoptestsrv stop success[[email protected] rc.d]# service testsrv restarttestsrv not starttestsrv restart success[[email protected] rc.d]# service testsrv statustestsrv is running[[email protected] rc.d]# service testsrv statfsdfsfargues error,please input start|restart|stop|status
本文出自 “學無止境” 部落格,請務必保留此出處http://dashui.blog.51cto.com/11254923/1851747
編寫CentOS 5,6下的服務指令碼