Ubuntu 16.04設置rc.local開機啓動命令/腳本的方法(通過update-rc.d管理Ubuntu開機啓動程序/服務)
注意:rc.local腳本里面啓動的用戶默認爲root權限。
一、rc.local腳本
rc.local腳本是一個Ubuntu開機後會自動執行的腳本,我們可以在該腳本內添加命令行指令。該腳本位於/etc/路徑下,需要root權限才能修改。
該腳本具體格式如下:
#!/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之前。裏面可以直接寫命令或者執行Shell腳本文件sh。
二、關於放在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、如果是執行sh文件,那麼要賦予執行權限sudo chmod +x xxx.sh,然後啓動時加上sudo sh xxx.sh
三、 update-rc.d增加開機啓動服務
給Ubuntu添加一個開機啓動腳本,操作如下:
1、新建個腳本文件new_service.sh
#!/bin/bash # command content exit 0
2、設置權限
sudo chmod 755 new_service.sh #或者 sudo chmod +x new_service.sh
3、把腳本放置到啓動目錄下
sudo mv new_service.sh /etc/init.d/
4、將腳本添加到啓動腳本
執行如下指令,在這裏90表明一個優先級,越高表示執行的越晚
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來管理上面啓動服務的啓動級別等,還是開機不啓動
sudo sysv-rc-conf
7、update-rc.d的詳細參數
使用update-rc.d命令需要指定腳本名稱和一些參數,它的格式看起來是這樣的(需要在 root 權限下):
update-rc.d [-n] [-f] <basename> remove update-rc.d [-n] <basename> defaults update-rc.d [-n] <basename> disable|enable [S|2|3|4|5] update-rc.d <basename> start|stop <NN> <runlevels> -n: not really -f: force
其中:
- disable|enable:代表腳本還在/etc/init.d中,並設置當前狀態是手動啓動還是自動啓動。
- start|stop:代表腳本還在/etc/init.d中,開機,並設置當前狀態是開始運行還是停止運行。(啓用後可配置開始運行與否)
- NN:是一個決定啓動順序的兩位數字值。(例如90大於80,因此80對應的腳本先啓動或先停止)
- runlevels:則指定了運行級別。
實例:
(1)、添加一個新的啓動腳本sample_init_script,並且指定爲默認啓動順序、默認運行級別(還記得前面說的嗎,首先要有實際的文件存在於/etc/init.d,即若文件/etc/init.d/sample_init_script不存在,則該命令不會執行):
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,指定默認運行級別,但啓動順序爲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相關組件開機自啓:
update-rc.d -f apache2 remove update-rc.d -f mysql remove
8、服務的啓動停止狀態
#通過service,比如 sudo service xxx status sudo service xxx start sudo service xxx stop sudo service xxx restart
9、查看全部服務列表
sudo service --status-all