編寫CentOS 5,6下的服務指令碼

來源:互聯網
上載者:User

標籤:服務指令碼

概述: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下的服務指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.