linux設(shè)置開機(jī)啟動有幾種方式,今天就來討論以下幾種方式。 |
方法一:編輯rc.loacl腳本
直接在/etc/rc.local中添加啟動腳本。
$ vim /etc/rc.local
方法二:Linux通過chkconfig設(shè)置開機(jī)啟動服務(wù)
創(chuàng)建腳本
vim /etc/init.d/bootshell
編輯腳本內(nèi)容如下(腳本前面三行格式固定)
#!/bin/sh
#chkconfig: 2345 100 101
#description:bootshell
case "$1" in
start) echo "start service" ;;
stop) echo "stop service" ;;
esac
說明:
第一行,告訴系統(tǒng)使用的shell,所有的shell腳本都是這樣。
第二行,chkconfig后面有三個參數(shù)2345,100和101告訴chkconfig程序,需要在rc2.d~rc5.d目錄下,創(chuàng)建名字為 S100bootshell的文件連接,連接到/etc/rc.d/init.d目錄下的的test2腳本。第一個字符是S,系統(tǒng)在啟動的時候,運(yùn)行腳本bootshell,就會添加一個start參數(shù),告訴腳本,現(xiàn)在是啟動模式。同時在rc0.d和rc6.d目錄下,創(chuàng)建名字為K90bootshell的 文件連接,第一個字符為K,系統(tǒng)在關(guān)閉系統(tǒng)的時候,會運(yùn)行bootshell,添加一個stop,告訴腳本,現(xiàn)在是關(guān)閉模式。 注意上面的三行中,第二,第三行是必須的,否則在運(yùn)行chkconfig --add bootshell時,會報錯。
運(yùn)行級別,越小越優(yōu)先運(yùn)行,優(yōu)先級相同的時候按創(chuàng)建時間啟動。
給文件設(shè)置執(zhí)行權(quán)限
chmod +x /etc/init.d/bootshell
測試腳本
/etc/init.d/bootshell start
chkconfig創(chuàng)建服務(wù)
chkconfig --add bootshell
此時rc2.d-rc5.d文件夾下已有創(chuàng)建好的文件。
檢驗(yàn)配置
chkconfig --list
刪除服務(wù)
chkconfig --del bootshell
關(guān)閉Linux的某個開機(jī)自啟動服務(wù)
chkconfig --level 2345 bootshell off
在centos中服務(wù)啟動腳本放置在:/etc/rc.d/init.d 而/etc/init.d這個目錄為公認(rèn)的目錄,在centos中/etc/init.d就是一個鏈接檔案/etc/sysconfig 服務(wù)初始化環(huán)境變量配置都在這個檔案中。
Linux設(shè)置開機(jī)啟動 | 《Linux就該這么學(xué)》 (linuxprobe.com)