使Linux系統上的程式開機後自動運行

來源:互聯網
上載者:User

   Linux有自己一套完整的啟動體系,抓住了Linux啟動的脈絡,Linux的啟動過程將不再神秘。

  閱讀之前建議先看一下附圖。

  本文中假設inittab中設定的init tree為:

  /etc/rc.d/rc0.d

  /etc/rc.d/rc1.d

  /etc/rc.d/rc2.d

  /etc/rc.d/rc3.d

  /etc/rc.d/rc4.d

  /etc/rc.d/rc5.d

  /etc/rc.d/rc6.d

  /etc/rc.d/init.d

  1. 關於Linux的啟動

  init是所有進程之父

  init讀取/etc/inittab,執行rc.sysinit指令碼

  (注意檔案名稱是不一定的,有些unix甚至會將語句直接寫在inittab中)

  rc.sysinit指令碼作了很多工作:

  init $PATH

  config network

  start swap function

  set hostname

  check root file system, repair if needed

  check root space

  ....

  rc.sysinit根據inittab執行rc?.d指令碼

  Linux是多使用者系統,getty是多使用者與單使用者的分水嶺

  在getty之前啟動並執行是系統指令碼

  . 關於rc.d

  所有啟動指令碼放置在 /etc/rc.d/init.d下

  rc?.d中放置的是init.d中指令碼的連結,

  命名格式是:

  S{number}{name}

  K{number}{name}

  S開始的檔案向指令碼傳遞start參數

  K開始的檔案向指令碼傳遞stop參數

  number決定執行的順序

  3. 啟動指令碼樣本

  這是一個用來啟動httpd的 /etc/rc.d/init.d/apache 指令碼:

  代碼:

  #!/bin/bash

  source /etc/sysconfig/rc

  source $rc_functions

  case "$1" in

  start)

  echo "Starting Apache daemon..."

  /usr/local/apache2/bin/apachectl -k start

  evaluate_retval

  ;;

  stop)

  echo "Stopping Apache daemon..."

  /usr/local/apache2/bin/apachectl -k stop

  evaluate_retval

  ;;

  restart)

  echo "Restarting Apache daemon..."

  /usr/local/apache2/bin/apachectl -k restart

  evaluate_retval

  ;;

  status)

  statusproc /usr/local/apache2/bin/httpd

  ;;

  *)

  echo "Usage: $0 {start|stop|restart|status}"

  exit 1

  ;;

  esac可以看出他接受start,stop,restart,status參數

  然後可以這樣建立rc?.d的連結:

  代碼:

  cd /etc/rc.d/init.d &&

  ln -sf ../init.d/apache ../rc0.d/K28apache &&

  ln -sf ../init.d/apache ../rc1.d/K28apache &&

  ln -sf ../init.d/apache ../rc2.d/K28apache &&

  ln -sf ../init.d/apache ../rc3.d/S32apache &&

  ln -sf ../init.d/apache ../rc4.d/S32apache &&

  ln -sf ../init.d/apache ../rc5.d/S32apache &&

  ln -sf ../init.d/apache ../rc6.d/K28apache4. 關於rc.local

  經常使用的 rc.local 則完全是習慣問題,不是標準。

  各個發行版有不同的實現方法,可以這樣實現:

  代碼:

  touch /etc/rc.d/rc.local

  chmod +x /etc/rc.d/rc.local

  ln -sf /etc/rc.d/rc.local /etc/rc.d/rc1.d/S999rc.local &&

  ln -sf /etc/rc.d/rc.local /etc/rc.d/rc2.d/S999rc.local &&

  ln -sf /etc/rc.d/rc.local /etc/rc.d/rc3.d/S999rc.local &&

  ln -sf /etc/rc.d/rc.local /etc/rc.d/rc4.d/S999rc.local &&

  ln -sf /etc/rc.d/rc.local /etc/rc.d/rc5.d/S999rc.local &&

  ln -sf /etc/rc.d/rc.local /etc/rc.d/rc6.d/S999rc.local5. 關於bash啟動指令碼

  /etc/profile

  /etc/bashrc

  ~/.bash_profile

  ~/.bashrc

  是bash的啟動指令碼

  一般用來設定單使用者的啟動環境,也可以實現開機單使用者的程式,但要明確他們都是屬於bash範疇而不是系統範疇。

  他們的具體作用介紹如下:

  /bin/bash這個命令解釋程式(後面簡稱shell)使用了一系列開機檔案來建立一個運行環境:

  /etc/profile

  /etc/bashrc

  ~/.bash_profile

  ~/.bashrc

  ~/.bash_logout

  每一個檔案都有特殊的功用並對登陸和互動環境有不同的影響。

  /etc/profile 和 ~/.bash_profile 是在啟動一個互動登陸shell的時候被調用。

  /etc/bashrc 和 ~/.bashrc 是在一個互動的非登陸shell啟動的時候被調用。

  ~/.bash_logout 在使用者登出登陸的時候被讀取

  一個互動的登陸shell會在 /bin/login 成功登陸之後運行。一個互動的非登陸shell是通過命令列來啟動並執行,如[prompt] $/bin/bash。一般一個非互動的shell出現在運行shell指令碼的時候。之所以叫非互動的shell,是因為它不在命令列上等待輸入而只是執行指令碼程式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.