博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux 系统rc.d启动脚本剖析
阅读量:2457 次
发布时间:2019-05-10

本文共 5279 字,大约阅读时间需要 17 分钟。

linux有自己一套完整的启动体系,抓住了linux启动的脉络,linux的启动过程将不再神秘。

本文中假设inittab中设置的init tree为:

(0-6对应系统的7个运行级别)

/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下

[root@Coohx rc.d]# ll init.d/总用量 256-rwxr-xr-x. 1 root root  3580 10月 15 2014 auditd-rwxr-xr-x. 1 root root  5221 7月  24 2015 cgconfig-rwxr-xr-x. 1 root root  3580 7月  24 2015 cgred-rwxr-xr-x. 1 root root  2826 3月  30 2015 crond-rwxr-xr-x  1 root root  1734 2月   9 19:49 dnsmasq-rwxr-xr-x  1 root root  3245 7月   9 2013 firstboot-rw-r--r--. 1 root root 19697 4月  10 2015 functions-rwxr-xr-x  1 root root  1801 10月 15 2014 haldaemon-rwxr-xr-x. 1 root root  5929 4月  10 2015 halt-rwxr-xr-x. 1 root root 11169 7月  24 2015 ip6tables-rwxr-xr-x. 1 root root 11048 7月  24 2015 iptables-rwxr-xr-x. 1 root root 19499 7月  24 2015 kdump-rwxr-xr-x. 1 root root   652 4月  10 2015 killall-rwxr-xr-x. 1 root root  2571 5月  20 2015 mdmonitor-rwxr-xr-x  1 root root  2200 4月  22 2015 messagebus-rwxr-xr-x. 1 root root  2989 4月  10 2015 netconsole

rc?.d中放置的是init.d中脚本的链接,命名格式是:

[root@Coohx rc.d]# ll rc0.d/总用量 0lrwxrwxrwx  1 root root 17 3月  17 00:36 K05wdaemon -> ../init.d/wdaemonlrwxrwxrwx. 1 root root 19 3月   5 23:37 K10saslauthd -> ../init.d/saslauthdlrwxrwxrwx. 1 root root 18 3月   5 23:38 K15svnserve -> ../init.d/svnservelrwxrwxrwx  1 root root 14 3月   6 03:45 K25sshd -> ../init.d/sshdlrwxrwxrwx. 1 root root 17 3月   5 23:37 K30postfix -> ../init.d/postfixlrwxrwxrwx  1 root root 24 3月  17 00:36 K30spice-vdagentd -> ../init.d/spice-vdagentdlrwxrwxrwx  1 root root 17 3月   6 03:44 K50dnsmasq -> ../init.d/dnsmasqlrwxrwxrwx  1 root root 15 4月   1 16:26 K50kdump -> ../init.d/kdumplrwxrwxrwx. 1 root root 17 3月   5 23:37 S00killall -> ../init.d/killalllrwxrwxrwx. 1 root root 14 3月   5 23:37 S01halt -> ../init.d/halt

S{number}{name} / K{number}{name}

  S开始的文件向脚本传递start参数

  K开始的文件向脚本传递stop参数

  number决定执行的顺序

3. 启动脚本示例

这是一个用来启动httpd的 /etc/rc.d/init.d/apache 脚本:

代码:

#!/bin/bash#!/bin/sh## chkconfig: 345 68 32......# one is reported.  Run "apachectl help" for usage info#ARGV="$@"## |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||# --------------------                              --------------------## the path to your httpd binary, including options if necessaryHTTPD='/usr/local/apache/bin/httpd'## pick up any necessary environment variablesif test -f /usr/local/apache/bin/envvars; then  . /usr/local/apache/bin/envvarsfi## a command that outputs a formatted text version of the HTML at the# url given on the command line.  Designed for lynx, however other# programs may work.LYNX="lynx -dump"## the URL to your server's mod_status status page.  If you do not# have one, then status and fullstatus will not work.STATUSURL="http://localhost:80/server-status"## Set this variable to a command that increases the maximum# number of file descriptors allowed per child process. This is# critical for configurations that use many file descriptors,# such as mass vhosting, or a multithreaded server.ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"# --------------------                              --------------------# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||# Set the maximum number of file descriptors allowed per child process.if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then    $ULIMIT_MAX_FILESfiERROR=0if [ "x$ARGV" = "x" ] ; then    ARGV="-h"ficase $ARGV instart|stop|restart|graceful|graceful-stop)    $HTTPD -k $ARGV    ERROR=$?    ;;startssl|sslstart|start-SSL)    echo The startssl option is no longer supported.    echo Please edit httpd.conf to include the SSL configuration settings    echo and then use "apachectl start".    ERROR=2    ;;configtest)    $HTTPD -t    ERROR=$?    ;;status)    $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '    ;;fullstatus)    $LYNX $STATUSURL    ;;*)    $HTTPD $ARGV    ERROR=$?esacexit $ERROR

可以看出他接受start,stop,restart,status参数

然后可以这样建立rc?.d的链接:

代码:

cd /etc/rc.d/init.d && llln -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. 关于bash启动脚本

/etc/profile

/etc/bashrc

~/.bash_profile

~/.bashrc

他们的具体作用介绍如下:

/bin/bash这个命令解释程序(后面简称shell)使用了一系列启动文件来建立一个运行环境:

/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等参数,是标准做法,推荐使用。

转载地址:http://wtnhb.baihongyu.com/

你可能感兴趣的文章
Java AWT TextField
查看>>
scala特质_Scala的特质
查看>>
python关键字和保留字_Python关键字
查看>>
合约 cd 模式_CD的完整形式是什么?
查看>>
vim中的jk为什么是上下_JK的完整形式是什么?
查看>>
oo0ooo0ooo0oo_OoO的完整形式是什么?
查看>>
kafka消息确认ack_什么是确认(ACK)? ACK代表什么?
查看>>
什么是Java文件?
查看>>
Java中的null是什么?
查看>>
json 语法_JSON的基本语法
查看>>
css中变量_CSS中的变量
查看>>
weakhashmap_Java WeakHashMap values()方法与示例
查看>>
java中访问修饰符_Java中的非访问修饰符是什么?
查看>>
kotlin键值对数组_Kotlin程序以升序对数组进行排序
查看>>
Java FileDescriptor sync()方法与示例
查看>>
Java PriorityQueue clear()方法与示例
查看>>
system getenv_Java System类getenv()方法及示例
查看>>
python 示例_带有示例的Python字典update()方法
查看>>
java 方法 示例_Java ArrayDeque offerFirst()方法与示例
查看>>
stringwriter_Java StringWriter toString()方法与示例
查看>>