1. 程式人生 > >Linux開機自動載入的幾種方法

Linux開機自動載入的幾種方法

linux 新增開機啟動項的三種方法。

(1)編輯檔案 /etc/rc.local

輸入命令:vim /etc/rc.local 將出現類似如下的文字片段:

#!/bin/sh

#This script will be executed after all the other init scripts.
#You can put your own initialization stuff in here if you don't
#want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
/etc/init.d/mysqld start #mysql開機啟動
/etc/init.d/nginx start #nginx開機啟動
/etc/init.d/php-fpm start #php-fpm開機啟動
/etc/init.d/memcached start #memcache開機啟動

#在檔案末尾(exit 0之前)加上你開機需要啟動的程式或執行的命令即可(執行的程式需要寫絕對路徑,新增到系統環境變數的除外),如:

/usr/local/thttpd/sbin/thttpd -C /usr/local/thttpd/etc/thttpd.conf

(2)自己寫一個shell指令碼

將寫好的指令碼(.sh檔案)放到目錄 /etc/profile.d/ 下,系統啟動後就會自動執行該目錄下的所有shell指令碼。

(3)通過chkconfig命令設定

將啟動檔案cp到 /etc/init.d/或者/etc/rc.d/init.d/(前者是後者的軟連線)下

vim 啟動檔案,檔案前面務必新增如下三行程式碼,否側會提示chkconfig不支援

#!/bin/sh 告訴系統使用的shell,所以的shell指令碼都是這樣
#chkconfig: 35 20 80 分別代表執行級別,啟動優先權,關閉優先權,此行程式碼必須
#description: http server(自己隨便發揮)//兩行都註釋掉!!!,此行程式碼必須

chkconfig --add 指令碼檔名 操作後就已經添加了