在CentOS中init.d service 開機時自動啟動指令碼

來源:互聯網
上載者:User

方法一,

編輯  /etc/rc.d/rc.local檔案

格式為  程式名  程式路徑

例如  a.sh  /home/a.sh

第二種方法

只需要將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 variables
prog_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 1

RETVAL=0

start(){
  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=1
esac

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

相關文章

聯繫我們

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