1. 程式人生 > >Linux 添加開機啟動項的三種方法

Linux 添加開機啟動項的三種方法

ash 環境變量 wan conf mysq exe usr eno bash

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

實例:

]:/etc/init.d# cat zhiyi.sh
#!/bin/bash #add for chkconfig #chkconfig: 2345 70 30 #description: the description of the shell #processname: zhiyi ip route add 192.168.0.0/24 via 172.20.18.1 ip route add 100.64.0.0/10 via 172.20.18.1 ip route add 172.31.13.0/24 via 172.20.18.1 ip route add 172.20.18.0/24 via 172.20.18.1

chkconfig --add zhiyi.sh

重啟後  查看是否生效

004]:/etc/init.d# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.20.18.102   0.0.0.0         UG    100    0        0 eno1
100.64.0.0      172.20.18.1     255.192.0.0     UG    0      0        0 eno1
172.20.0.0      0.0.0.0         255.255.0.0     U     100    0        0 eno1
172.20.18.0     172.20.18.1     255.255.255.0   UG    0      0        0 eno1
172.31.13.0     172.20.18.1     255.255.255.0   UG    0      0        0 eno1
192.168.0.0     172.20.18.1     255.255.255.0   UG    0      0        0 eno1

Linux 添加開機啟動項的三種方法