在CentOS上編寫init.d service script

來源:互聯網
上載者:User


背景:
之前編寫了一些指令碼,下載了一些開源軟體,想把它們做成系統服務,通過service your_prog_name start這樣的方式來後台運行,並在開機時自動啟動。
在瞭解了daemon命令之後,我發現使用它來編寫這樣的指令碼非常方便,相關樣本如下:

具體配置:
只需要將your_prog_name替換為你真正的指令碼或執行檔案的名稱,將prog_path設定為具體的路徑即可。
# vim /etc/init.d/your_prog_name

#!/bin/bash## Comments to support chkconfig# chkconfig: - 98 02# description: your_prog_name service script## Source function library.. /etc/init.d/functions### Default variablesprog_name="your_prog_name"prog_path="/usr/bin/${prog_name}"pidfile="/var/run/${prog_name}.pid"options="-c your_conf_file"# Check if requirements are met[ -x "${prog_path}" ] || exit 1RETVAL=0start(){  echo -n $"Starting $prog_name: "  daemon $prog_path $options  RETVAL=$?  PID=$(pidof ${prog_path})  [ ! -z "${PID}" ] && echo ${PID} > ${pidfile}  echo  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog_name  return $RETVAL}stop(){  echo -n $"Shutting down $prog_name: "  killproc -p ${pidfile}  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog_name  return $RETVAL}restart() {  stop  start}case "$1" in  start)    start    ;;  stop)    stop    ;;  restart)    restart    ;;  status)    status $prog_path    RETVAL=$?    ;;  *)    echo $"Usage: $0 {start|stop|restart|status}"    RETVAL=1esacexit $RETVAL

# chmod +x /etc/init.d/your_prog_name
# chkconfig your_prog_name on


轉自:http://ju.outofmemory.cn/entry/95057


附加service tomcat1 start例子:


#!/bin/sh
#
# chkconfig: 2345 85 21
# description: zimg startup scripts
#
tomcat="/usr/server/tomcat1"
startup="/usr/server/tomcat1/bin/startup.sh"
shutdown="/usr/server/tomcat1/bin/shutdown.sh"
export JAVA_HOME=/usr/local/jdk1.7.0
if [ -f /etc/rc.d/init.d/functions ]; then
        . /etc/rc.d/init.d/functions
fi

start() {
        cd $tomcat
        $startup
        echo "tomcat1 started."
}

stop() {

        cd $tomcat
        $shutdown
        echo "tomcat1 stopped."

}
# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        ;;
    reload)
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        ;;
esac
exit $RETVAL
~
~
~
~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.