1. 程式人生 > >CentOS 開機自啟動指令碼

CentOS 開機自啟動指令碼

開機時執行自己的指令碼. 1.編寫自己的服務指令碼 進入系統服務指令碼目錄: cd /etc/rc.d/init.d/ vi test 內容如下: #!/bin/bash # # chkconfig: - 57 75 # description: test service start() { echo "Starting test ..." echo 3 >> /tmp/sunyu.txt } stop() { echo "Stopping test ..." echo 2 >> /tmp/sunyu.txt } # See how we were called. case "$1" in   start) start ;;   stop) stop ;;   restart|force-reload) stop start ;;   *) echo $"Usage: $0 {start|stop|restart|force-reload}" exit 2 esac 要注意的是, 檔案的格式要是 unix. 可以通過 VI 命令 :set ff? 檢視.如果不是, 執行的時候會報錯: /bin/bash^M: bad interpreter VI 中儲存 將該指令碼設定為可執行: chmod +x test
可以看到. 啟動服務的時候會輸出一行字. 然後往 /tmp/sunyu.txt 中寫入一個 3. 關閉時會往檔案中寫入一個 2 如果該檔案不存在, 請先行自己在 /tmp 下新建:  cd /tmp/ touch sunyu.txt 然後將檔案設定為可寫: chmod 777 sunyu.txt 2.新增系統服務 chkconfig --add test 3.系統服務自啟動 chkconfig --level 345 test on 4.測試 service test start 然後檢視 /tmp/sunyu.txt 中的內容: cat /tmp/sunyu.txt service test stop 然後再檢視上面檔案中的內容 重啟: reboot 再檢視 /tmp/sunyu.txt 檔案中的內容.如果看到變化表示服務新增成功. 此例通過後, 可在指令碼中的 start stop 中做自己想做的其它事了.