ubuntu開機啓動

Ubuntu 16.04設置rc.local開機啓動命令/腳本的方法

Ubuntu 16.04設置rc.local開機啓動命令/腳本的方法(經過update-rc.d管理Ubuntu開機啓動程序/服務)mysql

注意:rc.local腳本里面啓動的用戶默認爲root權限。linux

1、rc.local腳本sql

rc.local腳本是一個Ubuntu開機後會自動執行的腳本,咱們能夠在該腳本內添加命令行指令。該腳本位於/etc/路徑下,須要root權限才能修改。apache

該腳本具體格式以下:bash

#!/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。memcached

2、關於放在rc.local裏面時不啓動的問題:this

一、能夠先增長日誌輸出功能,來查看最終爲何這個腳本不啓動的緣由,這個是Memcached啓動時的樣例文件:spa

#!/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

二、rc.local文件頭部/bin/sh修改成/bin/bash命令行

三、若是是執行sh文件,那麼要賦予執行權限sudo chmod +x xxx.sh,而後啓動時加上sudo sh xxx.shrest

3、 update-rc.d增長開機啓動服務

給Ubuntu添加一個開機啓動腳本,操做以下:

一、新建個腳本文件new_service.sh

#!/bin/bash
# command content exit 0

二、設置權限

sudo chmod 755 new_service.sh
#或者 sudo chmod +x new_service.sh

三、把腳本放置到啓動目錄下

sudo mv new_service.sh /etc/init.d/

四、將腳本添加到啓動腳本

執行以下指令,在這裏90代表一個優先級,越高表示執行的越晚

cd /etc/init.d/ sudo update-rc.d new_service.sh defaults 90

五、移除Ubuntu開機腳本

sudo update-rc.d -f new_service.sh remove

六、經過sysv-rc-conf來管理上面啓動服務的啓動級別等,仍是開機不啓動

sudo sysv-rc-conf 

七、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)、安裝兩個啓動腳本AB,讓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.dsample_init_script的連接(可能存在多個連接文件),update-rc.d只不過簡化了這一步驟。

(5)禁止Apache/MySQL相關組件開機自啓:

update-rc.d -f apache2 remove
update-rc.d -f mysql remove

八、服務的啓動中止狀態

#經過service,好比
sudo service xxx status
sudo service xxx start sudo service xxx stop sudo service xxx restart

九、查看所有服務列表

sudo service --status-all
相關文章
相關標籤/搜索