linux /etc/rc.d/init.d自启动程序说明

本文深入解析Linux启动体系,从init进程、rc.d目录结构到开机程序自动启动方式,全面覆盖Linux启动过程,揭示其背后的机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载地址:http://www.cnblogs.com/diyunpeng/archive/2009/11/11/1600886.html

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

========================================================================
设置系统自动启动
在/etc/init.d/下创建smsafe文件

内容:
#!/bin/bash
# chkconfig: 35 95 1
# description: script to start/stop smsafe
case $1 in
start)
sh /opt/startsms.sh
;;
stop)
sh /opt/stopsms.sh
;;
*)
echo "Usage: $0 (start|stop)"
;;
esac
更改权限
# chmod 775 smsafe

加入自动启动
# chkconfig –add smsafe

查看自动启动设置
# chkconfig –list smsafe

smsafe 0:off 1:off 2:off 3:on 4:off 5:on 6:off

以后可以用以下命令启动和停止脚本
# service smsafe start 启动

# service smsafe stop 停止

=======================================================================
jira 的启动主要依靠的是bin目录下的catalina.sh脚本,提供了如init脚本的start,stop等参数

#!/bin/bash

#

# chkconfig: 2345 85 15

# description: jira

# processname: jira

# source function library

. /etc/init.d/functions

#下面一行比较重要,为jira的安装路径,没有的话,将会提示找不到文件

CATALINA_HOME="/var/www/jira"

RETVAL=0

start() {

echo -n $"Starting jira services: "

. /var/www/jira/bin/catalina.sh start

RETVAL=$?

echo

}

stop() {

echo -n $"Shutting down jira services: "

. /var/www/jira/bin/catalina.sh stop

RETVAL=$?

echo

}

case "$1" in

start)

start

;;

stop)

stop

;;

restart|reload)

stop

start

;;

status)

status jira

RETVAL=$?

;;

*)

echo $"Usage: $0 {start|stop|restart|status}"

exit 1

esac

exit $RETVAL

-------------------------------

保存为/etc/init.d/jira

然后利用chkconfig --add jira

OK

启动/etc/init.d/jira start

停止/etc/init.d/jira stop

=======================================================================

(以Websphere为例子)

1. 在/etc/rc.d/init.d目录下新建启动脚本startWebsphere,键入以下内容:
#!/bin/sh
/opt/WebSphere/AppServer/bin/startServer.sh server1

修改该文件的权限:
chmod 755 startWebsphere

2. 在对应的目录下建立软连接(假设系统默认进入X11)
cd /etc/rc.d/rc5.d
ln -s ../init.d/startWebsphere S99startWebsphere

3. 重启系统即可

=======================================================================
linux下oracle的自启动脚本

1.写一个StartOracle.sql,假设放在/目录下 
vi /StartOracle.sql加入如下两行保存 
startup 
exit 

2.配置/etc/rc.local 
vi /etc/rc.local加入如下内容,保存 
su - oracle -c '$ORACLE_HOME/bin/lsnrctl start' 
su - oracle -c '$ORACLE_HOME/bin/sqlplus "/as sysdba" @/StartOracle.sql' 

3. 如果还要自动启动oracle enterprise manager(em)和isqlplus可以如下配置 
vi /etc/rc.local 加入: 
su - oracle -c '$ORACLE_HOME/bin/emctl start dbconsole' 
su - oracle -c '$ORACLE_HOME/bin/isqlplusctl start' 

要知道em和isqlplus等使用的端口可以查询文件: 
$ORACLE_HOME/install/portlist.ini(以oracle 10.1.0.3为例)

=======================================================================
#root命令行下直接绑定演示:
arp -s 192.x.x.x. 00:ea:se绑定.
arp -d 删除
arp -f 批量导入


<think>好的,我现在需要解释用户提供的这两个命令:`chmod +x /etc/rc.d/rc.local` 和 `echo "/etc/init.d/sshd.init start" >> /etc/rc.d/rc.local`。首先,我得确保自己完全理解每个命令的作用,以及它们通常用在什么场景下。 首先看第一个命令:`chmod +x /etc/rc.d/rc.local`。`chmod`是用来修改文件权限的命令,`+x`表示添加可执行权限。所以这个命令的作用是让`/etc/rc.d/rc.local`文件变成可执行的。通常,在Linux系统中,某些文件默认可能没有执行权限,需要手动添加,特别是像启动脚本这样的文件。如果用户希望这个脚本在启动时自动运行,那么它必须具备可执行权限,否则系统无法执行它。 接下来是第二个命令:`echo "/etc/init.d/sshd.init start" >> /etc/rc.d/rc.local`。这里使用了`echo`命令将字符串`/etc/init.d/sshd.init start`追加到`/etc/rc.d/rc.local`文件中。`>>`是重定向符号,表示追加内容到文件末尾,而不是覆盖。这条命令的目的是将启动SSH服务的命令添加到`rc.local`脚本中,这样每次系统启动时,就会自动执行这个命令,启动SSH守护进程。 不过,现在需要考虑这些命令是否适用于现代的Linux发行版。因为我知道,很多系统已经转向使用systemd,而不是传统的SysVinit。例如,在基于systemd的系统中,`rc.local`可能默认不存在,或者需要额外启用。例如,用户可能需要先启用`rc-local.service`,并确保`/etc/rc.d/rc.local`有正确的权限和内容。如果用户是在一个仍然使用SysVinit或Upstart的系统上,比如较旧版本的CentOS或Debian,那么这些命令可能仍然有效。 另外,关于第二个命令中的路径`/etc/init.d/sshd.init`,这让我有点疑问。通常,SSH服务的启动脚本是`/etc/init.d/ssh`或者`/etc/init.d/sshd`,具体取决于发行版。例如,在Debian/Ubuntu中通常是`/etc/init.d/ssh`,而在CentOS/RHEL中是`/etc/init.d/sshd`。用户提到的`sshd.init`可能是一个非标准的脚本名称,或者可能是在某些特定发行版中的命名。这可能导致命令执行失败,如果路径不正确的话。需要确认该路径是否存在,否则用户可能需要调整路径。 此外,使用`rc.local`来管理启动项虽然传统,但在现代系统中可能不是最佳实践。使用systemd的话,用户应该创建一个服务单元文件,或者使用`systemctl enable`来启用服务。例如,对于SSH服务,通常已经存在对应的服务单元,用户只需执行`systemctl enable sshd`即可让SSH在启动时自动运行。如果用户坚持使用`rc.local`,可能需要检查systemd是否兼容,或者需要做其他配置。 还有一个需要注意的地方是权限问题。修改系统文件如`rc.local`需要超级用户权限,所以用户在执行这两个命令时应该使用`sudo`或者以root身份运行,否则可能会遇到权限拒绝的错误。例如,正确的命令应该是: ```bash sudo chmod +x /etc/rc.d/rc.local sudo echo "/etc/init.d/sshd.init start" >> /etc/rc.d/rc.local ``` 不过,当使用`sudo`配合`>>`重定向时,可能会遇到权限问题,因为重定向是由shell处理的,而不是`sudo`。所以更可靠的方式是用`tee`命令: ```bash echo "/etc/init.d/sshd.init start" | sudo tee -a /etc/rc.d/rc.local ``` 这样可以确保以root权限追加内容到文件。 另外,用户需要确认`/etc/rc.d/rc.local`是否存在。在某些系统中,这个文件可能位于`/etc/rc.local`,而`/etc/rc.d/rc.local`是一个符号链接。如果文件不存在,可能需要手动创建,并确保其可执行。 总结一下,用户的目的是在系统启动时自动启动SSH服务。但根据不同的系统环境,可能有不同的最佳实践。如果系统使用systemd,更推荐的方式是使用`systemctl enable sshd`。如果确实需要使用`rc.local`,则需要确保该机制在系统中是启用的,并且文件路径和启动命令正确无误。 可能的错误点包括: 1. `rc.local`文件不存在或未启用。 2. SSH启动脚本的路径不正确。 3. 权限不足,未使用sudo。 4. 在systemd系统中未处理依赖关系,导致`rc.local`未执行。 因此,在解释这两个命令时,需要指出它们的用途,同时提醒用户注意系统兼容性和更现代的替代方法。此外,验证步骤也很重要,比如检查`rc.local`是否执行,SSH服务是否确实在启动后运行。</think>这两个命令用于在 Linux 系统启动时自动启动 SSH 服务,但需注意**系统兼容性**和**现代替代方案**。以下是分步解释: --- ### 1. `chmod +x /etc/rc.d/rc.local` - **作用**:为 `/etc/rc.d/rc.local` 文件添加可执行权限。 - **背景**:`rc.local` 是传统 Linux 系统(如 SysVinit)的启动脚本,系统启动时会自动执行此文件。若文件无可执行权限,脚本将无法运行。 --- ### 2. `echo "/etc/init.d/sshd.init start" >> /etc/rc.d/rc.local` - **作用**:将启动 SSH 服务的命令追加到 `rc.local` 文件中。 - **背景**:通过追加命令 `/etc/init.d/sshd.init start`,系统每次启动时会自动执行此命令以启动 SSH 服务。 --- ### 注意事项 1. **系统兼容性**: - **现代系统(如 systemd)**:许多新发行版(Ubuntu 16.04+、CentOS 7+)默认使用 `systemd`,不再依赖 `rc.local`。若需使用: - 确保 `rc-local.service` 已启用: ```bash sudo systemctl enable rc-local.service ``` - 文件路径可能是 `/etc/rc.local`(而非 `/etc/rc.d/rc.local`)。 - **传统系统(SysVinit)**:适用于 CentOS 6、Debian 7 等旧版本。 2. **SSH 脚本路径问题**: - 常见路径为 `/etc/init.d/ssh`(Debian/Ubuntu)或 `/etc/init.d/sshd`(CentOS/RHEL)。 - 执行前需确认 `/etc/init.d/sshd.init` 是否存在,否则命令会失败。 3. **更推荐的现代方法**: - 使用 `systemd` 管理服务: ```bash sudo systemctl enable sshd # 启用开机自启 sudo systemctl start sshd # 立即启动 ``` - 此方法更标准化且易于维护。 4. **权限问题**: - 若未用 `sudo`,可能导致权限错误。建议完整命令为: ```bash sudo chmod +x /etc/rc.d/rc.local echo "/etc/init.d/sshd.init start" | sudo tee -a /etc/rc.d/rc.local ``` --- ### 验证是否生效 1. 重启系统后检查 SSH 服务状态: ```bash systemctl status sshd # systemd 系统 service sshd status # SysVinit 系统 ``` 2. 检查 `rc.local` 是否被执行: ```bash sudo grep "sshd.init" /var/log/boot.log # 查看启动日志 ``` --- ### 总结 - **适用场景**:传统 SysVinit 系统,且需确保路径和权限正确。 - **推荐更新方案**:优先使用 `systemctl enable sshd`(现代系统)。 - **风险提示**:直接修改 `rc.local` 可能在新系统中失效,建议确认系统初始化机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值