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 腳本文件名 操作后就已經添加了