rc.local开机自启

admin 2021年8月27日14:32:45评论46 views字数 2944阅读9分48秒阅读模式

搬运自mingy大佬的笔记

https://note.youdao.com/ynoteshare1/index.html?id=f73ac4ea46551d53adb86cca63821fa5&type=note

设置开机启动示例

1
touch /etc/init.d/rc.local

设置为可执行:

1
chmod +x /etc/init.d/rc.local

用 update-rc.d 设置启动级别:

1
update-rc.d rc.local start 99 2 3 4 5 . stop 99 0 1 6 .

为了编辑方便,创建一个链接:

1
2
ln -s /etc/init.d/rc.local /etc/rc.local
cat /etc/rc.local
1
2
3
4
5
6
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
## start apache
1
/usr/local/apache/bin/apachectl start

与 rc.local 的详细讲述:

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

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

1
2
3
4
5
6
7
8
/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 的启动

init 是所有进程的顶层

init 读取/etc/inittab,执行 rc.sysinit 脚本

(注意文件名是不一定的,有些 unix 甚至会将语句直接写在 inittab 中)

rc.sysinit 脚本作了很多工作:

1
2
3
4
5
6
7
8
9
10
11
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 中脚本的链接,命名格式是:

1
2
3
S{number}{name}

K{number}{name}

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

number 决定执行的顺序

3. 启动脚本示例**

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

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

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

代码:

1
2
3
4
5
6
7
8
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 则完全是习惯问题,不是标准。

各个发行版有不同的实现方法,可以这样实现:

代码:

1
2
3
4
5
6
7
8
9
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 启动脚本

1
2
3
4
/etc/profile
/etc/bashrc
~/.bash_profile
~/.bashrc

是 bash 的启动脚本一般用来设置单用户的启动环境,也可以实现开机单用户的程序,但要明确他们都是属于 bash 范畴而不是系统范

畴。

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

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

1
2
3
4
5
/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 启动脚本中。

FROM :https://ailumao.cn/ | Author:Ailumao

相关推荐: 密码保护:Nodejs安全从入门到入土

这是一篇受密码保护的文章,您需要提供访问密码: 密码: 相关推荐: Mybatis-plus框架常见SQL注入场景 一、关于Mybatis-plus   MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (op…

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年8月27日14:32:45
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   rc.local开机自启https://cn-sec.com/archives/475108.html

发表评论

匿名网友 填写信息