linux--ubuntu 16 自啓動 rc.local

1 簡介

在相應的啓動腳本中按照規則加上對應的啓動腳本語句,使用最多的啓動腳本是 rc.local 文件。大家習慣在rc.local文件中配置啓動信息(也可以在別的啓動腳本中配置)。

2 基礎知識

2.1 linux啓動流程

(1)加載BIOS的硬件信息與進行自我測試, 並依據設置取得第一個可啓動的設備;
(2)讀取並執行第一個啓動設備內MBR的boot loader(即grub、spfdisk等程序);
(3)一句boot loader 的設置加載Kernel,Kernel會開始檢測硬件與加載驅動程序;
(4)在硬件驅動成功後,Kernel會主動調用init進程,而 init 會取得 run-level 信息;
(5)init 執行/etc/rc.d/rc.sysinit 文件來準備軟件執行的操作環境(如網絡時區等);
(6)init 執行 run-level 的各個服務器的啓動(script方式);
(7)init 執行/etc/rc.d/rc.local 文件;
(8)init 執行終端機模擬程序 mingetty 來啓動login 進程,最後就等待用戶登陸。

3 自啓動設置步驟

4 還原配置

5 定位問題

5.1 rc.local是否執行

  • rc.local文件添加日誌輸出功能,看是否有日誌文件生成,若有,證明rc.local已經執行過
#!/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> /home/ubuntu/workspace/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 

#added by worthsen
myServer
#end

exit 0

5.2 rc.local是否執行失敗

  • 終端輸入該命令查看rc.local執行失敗的原因
 systemctl status rc.local

5.3 重啓rclocal命令

systemctl restart rc-local

可以通過這樣來檢測開機啓動是否能成功,而不是每次都要重啓,重啓!

6 注意事項

6.1 權限

要加上對應的執行權限

6.2 絕對路徑

儘量使用絕對路徑,~對應的/home/ubuntu有時會被代替爲/root,從而出錯

6.3 bash 代替 sh

#!/bin/sh
對應bin中的dash,版本老,有些新命令不支持,從而報錯
#!/bin/bash 推薦

6.4 rc.local中命令加權限

如下語句,如果腳本中需要sudo權限的操作會執行失敗

sh /run/media/mmcblk0p4/Sen/test_start.sh &

正確如下,注意設置管理用戶使用sudo命令免密模式

sudo  sh /run/media/mmcblk0p4/Sen/test_start.sh &

參考

1、理解Linux系統/etc/init.d目錄和/etc/rc.local腳本
2、linux–rc.local
3、linux–切換ubuntu啓動方式 及 還原配置
4、ubuntu 在 rc.local 裏添加了命令爲什麼無法執行?
5、Ubuntu 16.04設置rc.local開機啓動命令/腳本的方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章