Linux自启动设置详解

前言

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 

...... 

 

可以看出他接受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

 

附录二:在Red Hat Linux中自动运行程序

http://www.newebug.com/article/unix-linux/1428.shtml

 

在Red Hat Linux中自动运行程序

1.开机启动时自动运行程序

  Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程init。init根据配置文

件继续引导过程,启动其它进程。通常情况下,修改放置在 /etc/rc或 /etc/rc.d 或 /etc/rc?.d 

目录下的脚本文件,可以使init自动启动其它程序。例如:编辑 /etc/rc.d/rc.local 文件,在

文件最末加上一行"xinit"或"startx",可以在开机启动后直接进入X-Window。

 

  2.登录时自动运行程序

  用户登录时,bash首先自动执行系统管理员建立的全局登录script :/ect/profile。然后

bash在用户起始目录下按顺序查找三个特殊文件中的一个:/.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

 

  2000-11-09 12:00时候自动发一标题为"Have a lunch",内容为plan.txt文件内容的邮

件给Roger。

你可能感兴趣的:(apache,linux,shell,File,脚本,bash)