© / Posted in Linux / September 14, 2009

前言
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的启动
2. 关于rc.d
3. 启动脚本示例
4. 关于rc.local
5. 关于bash启动脚本
6. 关于开机程序的自动启动

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之前运行的是系统脚本

2. 关于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/K28apache

4. 关于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.local

5. 关于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,是因为它不在命令行上等待输入而只是执行脚本程序。

6. 关于开机程序的自动启动

  • 系统脚本可以放置在/etc/rc.d/init.d中并建立/etc/rc.d/rc?.d链接,也可以直接放置在/etc/rc.d/rc.local中。
    init.d脚本包含完整的start,stop,status,reload等参数,是标准做法,推荐使用。
  • 为特定用户使用的程序(如有的用户需要使用中文输入法而有的不需要)放置在~/中的bash启动脚本中。

本文有 8 篇评论 ↓↓

    1. 这个不太懂,记得火山的免费vps,win系列的很快就没了,而linux却到了晚上都能申请到,说明linux在草根中还没普及开

      1. @奔四大叔, 呵呵,是普及不算好,没有点计算机知识的用不来~~
        我也挺想弄火山那VPS的,不过这东西,不可能说真的就永久免费给你的~~肯定等他功能测试完了就给你撤了~~
        而且要求个人资料~~绝对像身份证之类的个人资料,还是不要轻易给人

    2. 火山目前免费从某种角度来说其实也是为了推广普及linux,等用的人多了他自然获益的机会就多了

    3. 这个东西我不懂
      纯属路过
      哈哈

    4. 最近我也在学Linux,Linux的命令太多,初学感觉有些困难,不过学PHP不学Linux是不行的~

      1. @wuleilei, 命令太多??不会吧,用到的就少数几个,然后用多了就记住了啊

    5. Linux的命令太多,初学感觉有些困难,不过学PHP不学Linux是不行的~Christian Louboutin 不错


    6. welcome to our burberry outlet store ,there are many fashion and hight quality burberry bags wait for

      you!
      do you want to buy burberry bags for yourslf or your beat friends in sometimes?

      please come to our burberry outlet store.
      our burberry outlet store offer many new style burberry watches for you.do

      not miss the fashion burbery Burberry T-shirts.
      In burberry sunglasses stores it really is typically some sort of

      most effective style which precisely the top notch and several middle-class are typically in a region to afford
      Fouthy-six Putting on a new burberry watches for men and relish the

      summertime Does one prefers to invest in Burberry bags?


      as we all know the burberry brand is very famouse in the world.in burberry bags outlet store you

      will have a good time!
      there is new store,the name is burberry outlet !you are interested in new store,you will have a good

      time in there.
      If you use burberry bags ,you will feel very Summer feeling, so many girls put it

      down.
      evey women love the burberry shirts in life,the burberry can give you more

      confidence in sometimes.
      Jack bought the burberry sunglasses and the next day he came to the

      interview in the Burberry cheak shirt.


      Join forums that areparticularlymeant to talk about watches. If you are searching for burberry sale for women, then

      obviouslycheck out women's forum.
      One of the most popular British exports is the burberry outlet online .
      The particular burberry bags typically reveal the actual Burberry renowned examine layout

      which has been made popular by the garment range.
      The color of burberry ties are bright but with the characters of mature and steady
      the burberry shoes collection had occipied a large markey share and enjoys a massive

      fan following.
      I believe one's destiny of your burberry wallets long decrease jackets are

      often more large-scale progress.

    添加新评论 ↑↑