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
hostname
check root file system, repair 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
......
可以看出他接受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启动脚本中
下面用自启动apache为例;
自启动脚本:
/usr/local/apache2/bin;
./apachectl start
文件位于/etc/rc.d/init.d下,名为apached, 注意要可执行.
#chmod +x /etc/rc.d/init.d/apached //设置文件属性为可执行
#ln -s /etc/rc.d/init.d/apached /etc/rc3.d/S90apache //建立软连接,快捷方式
#ln -s /etc/rc.d/init.d/apached /etc/rc0.d/K20apache
附录 2:在Red Hat Linux中自动运行
[url]http://www.ebug.com/article/unix-linux/1428.shtml[/url]
在Red Hat Linux中自动运行
1.开机启动时自动运行
Linux加载后, 它将化硬件和设备驱动, 然后运行第个进程initinit根据配置文件继续引导过程启动其它进程通常情况下修改放置在 /etc/rc或 /etc/rc.d 或 /etc/rc?.d 目录下脚本文件可以使init自动启动其它例如:编辑 /etc/rc.d/rc.local 文件在文件最末加上行"xinit"或"startx"可以在开机启动后直接进入X-Window
2.登录时自动运行
用户登录时bash首先自动执行系统管理员建立全局登录script :/ect/profile然后bash在用户起始目录下按顺序查找 3个特殊文件中个:/.bash_profile、/.bash_login、 /.profile但只执行最先找到个
因此只需根据实际需要在上述文件中加入命令就可以实现用户登录时自动运行某些(类似于DOS下Autoexec.bat)
3.退出登录时自动运行
退出登录时bash自动执行个人退出登录脚本/.bash_logout例如在/.bash_logout中加入命令"tar -cvzf c.source.tgz *.c"则在每次退出登录时自动执行 "tar" 命令备份 *.c 文件
4.定期自动运行
Linux有个称为crond守护主要功能是周期性地检查 /var/spool/cron目录下组命令文件内容并在设定时间执行这些文件中命令用户可以通过crontab 命令来建立、修改、删除这些命令文件
例如建立文件crondFile内容为"00 9 23 Jan * HappyBirthday"运行"crontab cronFile"命令后每当元月23日上午9:00系统自动执行"HappyBirthday"("*"表示不管当天是星期几)
5.定时自动运行次
定时执行命令at 和crond 类似(但它只执行次):命令在给定时间执行但不自动重复at命令般格式为:at [ -f file ] time 在指定时间执行file文件中所给出所有命令也可直接从键盘输入命令:
$ at 12:00
at>mailto Roger -s ″Have a lunch″ < plan.txt
at>Ctr-D
Job 1 at 2000-11-09 12:00
最新评论