《LINUX實操:Ubuntu 16.04設置rc.local開機啟動命令/腳本的方法》要點:
本文介紹了LINUX實操:Ubuntu 16.04設置rc.local開機啟動命令/腳本的方法,希望對您有用。如果有疑問,可以聯系我們。
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
此中:
實例:
(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
更多Ubuntu相關信息見Ubuntu 專題頁面 /topicnews.aspx?tid=2
本文永遠更新鏈接地址:
更多LINUX教程,盡在維易PHP學院專欄。歡迎交流!
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/6553.html