前言
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 脚本 :

CODE:


#!/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的链接:

CODE:


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 则完全是习惯问题,不是标准。
各个发行版有不同的实现方法,可以这样实现:

CODE:


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启动 脚本 中。

最新文章

  1. js 判断IE浏览器,包含IE6/7/8/9
  2. ie8的兼容
  3. Swift2.1 语法指南——类型转换
  4. SPFA导读及介绍(转载)
  5. linux下安装jdk的几种方式
  6. 深入分析Java Web中的中文编码问题
  7. Maven使用笔记(四)pom.xml配置详解
  8. C# 定义常量 两种实现方法
  9. Android从零单排之自动跟新
  10. POJ 3671 Dining Cows (DP,LIS, 暴力)
  11. WebService 学习总结
  12. SGU 143.Long Live the Queen(女王万岁)
  13. nodejs原生模块简介
  14. [bzoj1003][ZJOI2006][物流运输] (最短路+dp)
  15. 关于mwArray和一般数组的区别
  16. JavaSE学习总结第11天_开发工具 & API常用对象1
  17. REST风格架构
  18. ProxySQL Cluster 高可用集群环境部署记录
  19. Vivado约束文件(XDC)的探究(1)
  20. LVS+Heartbeat 高可用集群方案操作记录

热门文章

  1. 跳跃表-原理及Java实现
  2. ng-model 和ng-bind的区别
  3. mysql 时间索引执行计划
  4. Ajax请求参数到一个URL包含下划线或者v(_、v)
  5. vue-router的hash模式和history模式,
  6. java agent问题
  7. BOM和DOM的操作
  8. React 项目中修改 Ant Design 的默认样式(Input Checkbox 等等
  9. Spring Boot教程(六)在springboot中验证表单信息
  10. Spring Boot教程(三)消费Restful的web服务