新聞中心
ubuntu 16.04設(shè)置rc.local開機啟動命令/腳本的方法(通過update-rc.d管理Ubuntu開機啟動程序/服務(wù))

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序定制開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了無為免費建站歡迎大家使用!
一、rc.local腳本
rc.local腳本是一個Ubuntu開機后會自動執(zhí)行的腳本,我們可以在該腳本內(nèi)添加命令行指令。該腳本位于/etc/路徑下,需要root權(quán)限才能修改。
該腳本具體格式如下:
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. exit 0
注意: 一定要將命令添加在exit 0之前。里面可以直接寫命令或者執(zhí)行Shell腳本文件sh。
二、關(guān)于放在rc.local里面時不啟動的問題:
1、可以先增加日志輸出功能,來查看最終為什么這個腳本不啟動的原因,這個是Memcached啟動時的樣例文件:
#!/bin/sh -e # # rc.local # # This script is executed at the end of each multiuser runlevel. # Make sure that the script will "exit 0" on success or any other # value on error. # # In order to enable or disable this script just change the execution # bits. # # By default this script does nothing. #log exec 2> /tmp/rc.local.log # send stderr from rc.local to a log file exec 1>&2 # send stdout to the same log file set -x # tell sh to display commands before execution #Memcached /usr/local/memcache/bin/memcached -p 11211 -m 64m -d -u root exit 0
2、rc.local文件頭部/bin/sh修改為/bin/bash
3、如果是執(zhí)行sh文件,那么要賦予執(zhí)行權(quán)限sudo chmod +x xxx.sh,然后啟動時加上sudo sh xxx.sh
三、 update-rc.d增加開機啟動服務(wù)
給Ubuntu添加一個開機啟動腳本,操作如下:
1、新建個腳本文件new_service.sh
#!/bin/bash # command content exit 0
2、設(shè)置權(quán)限
sudo chmod 755 new_service.sh #或者 sudo chmod +x new_service.sh
3、把腳本放置到啟動目錄下
sudo mv new_service.sh /etc/init.d/
4、將腳本添加到啟動腳本
執(zhí)行如下指令,在這里90表明一個優(yōu)先級,越高表示執(zhí)行的越晚
cd /etc/init.d/ sudo update-rc.d new_service.sh defaults 90
5、移除Ubuntu開機腳本
sudo update-rc.d -f new_service.sh remove
6、通過sysv-rc-conf來管理上面啟動服務(wù)的啟動級別等,還是開機不啟動
sudo sysv-rc-conf
7、update-rc.d的詳細(xì)參數(shù)
使用update-rc.d命令需要指定腳本名稱和一些參數(shù),它的格式看起來是這樣的(需要在 root 權(quán)限下):
update-rc.d [-n] [-f]remove update-rc.d [-n] defaults update-rc.d [-n] disable|enable [S|2|3|4|5] update-rc.d start|stop -n: not really -f: force
其中:
- disable|enable:代表腳本還在/etc/init.d中,并設(shè)置當(dāng)前狀態(tài)是手動啟動還是自動啟動。
- start|stop:代表腳本還在/etc/init.d中,開機,并設(shè)置當(dāng)前狀態(tài)是開始運行還是停止運行。(啟用后可配置開始運行與否)
- NN:是一個決定啟動順序的兩位數(shù)字值。(例如90大于80,因此80對應(yīng)的腳本先啟動或先停止)
- runlevels:則指定了運行級別。
實例:
(1)、添加一個新的啟動腳本sample_init_script,并且指定為默認(rèn)啟動順序、默認(rèn)運行級別(還記得前面說的嗎,首先要有實際的文件存在于/etc/init.d,即若文件/etc/init.d/sample_init_script不存在,則該命令不會執(zhí)行):
update-rc.d sample_init_script defaults
上一條命令等效于(中間是一個英文句點符號):
update-rc.d sample_init_script start 20 2 3 4 5 . stop 20 0 1 6
(2)、安裝一個啟動腳本sample_init_script,指定默認(rèn)運行級別,但啟動順序為50:
update-rc.d sample_init_script defaults 50
(3)、安裝兩個啟動腳本A、B,讓A先于B啟動,后于B停止:
update-rc.d A 10 40 update-rc.d B 20 30
(4)、刪除一個啟動腳本sample_init_script,如果腳本不存在則直接跳過:
update-rc.d -f sample_init_script remove
這一條命令實際上做的就是一一刪除所有位于/etc/rcX.d目錄下指向/etc/init.d中sample_init_script的鏈接(可能存在多個鏈接文件),update-rc.d只不過簡化了這一步驟。
(5)禁止Apache/MySQL相關(guān)組件開機自啟:
update-rc.d -f apache2 remove update-rc.d -f mysql remove
8、服務(wù)的啟動停止?fàn)顟B(tài)
#通過service,比如 sudo service xxx status sudo service xxx start sudo service xxx stop sudo service xxx restart
9、查看全部服務(wù)列表
sudo service --status-all
當(dāng)前文章:Ubuntu16.04設(shè)置rc.local開機啟動命令/腳本的方法
網(wǎng)頁路徑:http://m.fisionsoft.com.cn/article/djpdigp.html


咨詢
建站咨詢
