249建站之家:UBUNTU16.04启动顺序与开机自动脚本编写

ubuntu的启动顺序

与大多数linux系统一样,ubuntu按照以下顺序启动:

1. 读取MBR信息,启动Boot Manager。

2. 加载系统内核,启动 init 进程, init 进程是 Linux 的根进程,所有的系统进程都是它的子进程。

3. init 进程读取 /etc/inittab 文件中的信息,并进入预设的运行级别。通常情况下/etc/rcS.d/ 目录下的启动脚本首先被执行,然后是/etc/rcN.d/ 目录。

4. 根据 /etc/rcS.d/文件夹中对应的脚本启动 Xwindow 服务器 xorg,Xwindow 为 Linux 下的图形用户界面系统。

5. 启动登录管理器,等待用户登录。


运行级别


ubuntu的自启动脚本目录为/etc/rc*,一般为rc0.d - rc6.d,对应操作系统0-6级运行状态下需要执行的脚本。此外还有rcS.d目录,rc.local文件。除了rc.local外,其它目录内的脚本都链接到/etc/init.d目录下的脚本。这就是为什么我们经常会用/etc/init.d/service start这样的命令来启动服务。


linux运行级别的解析


级别 命令 解析

0 Halt 关机模式

1 Single 单用户模式

2 Full multi-user with display manager (GUI) 多用户模式

3 Full multi-user with display manager (GUI) 多用户模式

4 Full multi-user with display manager (GUI) 多用户模式

5 Full multi-user with display manager (GUI) 多用户模式

6 Reboot 重启

通常情况下,运行级别下的启动程序的脚本在对应的rc0.d-rc6.d目录下。一般情况下,rc2.d-rc5.d目录下的启动脚本是一样的,因为它们都是同一级别的启动内容。

当前系统的运行级别可以通过runlevel命令查看。


jss@jss-inpractice:/etc$ runlevel

N 5

经查看,我们当前的启动级别是5,那么我们进入rc5.d目录,同时创建启动脚本,那么开机时就会自动启动该程序。不过普遍的我们都是在init.d目录下创建该脚本,然后通过软链接到rcN.d目录下。现在我们进入rc5.d目录下,看看里面的内容:


lrwxrwxrwx   1 root root    16 2月  10 09:31 S01apport -> ../init.d/apport*

lrwxrwxrwx   1 root root    17 2月  10 09:31 S01rsyslog -> ../init.d/rsyslog*

lrwxrwxrwx   1 root root    29 2月  10 09:31 S01unattended-upgrades -> ../init.d/unattended-upgrades*

lrwxrwxrwx   1 root root    15 2月  10 09:31 S01uuidd -> ../init.d/uuidd*

lrwxrwxrwx   1 root root    15 2月  10 09:31 S02acpid -> ../init.d/acpid*

lrwxrwxrwx   1 root root    17 2月  10 09:31 S02anacron -> ../init.d/anacron*

lrwxrwxrwx   1 root root    14 2月  10 09:31 S02cron -> ../init.d/cron*

lrwxrwxrwx   1 root root    14 2月  10 09:31 S02dbus -> ../init.d/dbus*

lrwxrwxrwx   1 root root    20 2月  10 09:31 S02irqbalance -> ../init.d/irqbalance*

lrwxrwxrwx   1 root root    20 2月  10 09:31 S02kerneloops -> ../init.d/kerneloops*

lrwxrwxrwx   1 root root    15 2月  10 09:31 S02rsync -> ../init.d/rsync*

lrwxrwxrwx   1 root root    27 2月  10 09:31 S02speech-dispatcher -> ../init.d/speech-dispatcher*

lrwxrwxrwx   1 root root    18 2月  10 09:31 S02thermald -> ../init.d/thermald*

lrwxrwxrwx   1 root root    18 2月  10 09:31 S02whoopsie -> ../init.d/whoopsie*

lrwxrwxrwx   1 root root    22 2月  10 09:31 S03avahi-daemon -> ../init.d/avahi-daemon*

lrwxrwxrwx   1 root root    19 2月  10 09:31 S03bluetooth -> ../init.d/bluetooth*

lrwxrwxrwx   1 root root    17 2月  10 09:31 S03lightdm -> ../init.d/lightdm*

lrwxrwxrwx   1 root root    14 2月  10 09:31 S04cups -> ../init.d/cups*

lrwxrwxrwx   1 root root    22 2月  10 09:31 S04cups-browsed -> ../init.d/cups-browsed*

lrwxrwxrwx   1 root root    15 2月  10 09:31 S04saned -> ../init.d/saned*

lrwxrwxrwx   1 root root    21 2月  10 09:31 S05grub-common -> ../init.d/grub-common*

lrwxrwxrwx   1 root root    18 2月  10 09:31 S05ondemand -> ../init.d/ondemand*

lrwxrwxrwx   1 root root    18 2月  10 09:31 S05plymouth -> ../init.d/plymouth*

lrwxrwxrwx   1 root root    18 2月  10 09:31 S05rc.local -> ../init.d/rc.local*

我们可以发现,该目录下的软链接名称,似乎有一定的规则:

S[number][service name] --> ../init.d/servicename


S 表示 Start,开启服务

[number]表示的是该脚本的运行优先级,number越小,脚本的运行优先级就越高

[service name]表示的是服务的名称

因此,我们要定义服务的启动顺序,只需要指定number大小即可。


开启与关闭服务


如果我们手动创建启动脚本,然后添加到对应级别的目录下,这个过程是比较繁琐的,幸好linux给我们提供了命令:update-rc.d

update-rc.d <service name> start|stop| <order number> <run levels>

所以我们只需要在init.d目录下创建好我们的脚本,然后使用update-rc.d命令,就可以添加到对应级别的启动过程。


示例:


# 服务名  启动/停止 顺序号 级别

sudo update-rc.d rinetd start 20 2

sudo update-rc.d rinetd stop 20 0

update-rc.d <service name> enable|disable <runlevels> # 开启或者是禁止


sudo update-rc.d rinetd disable 2 在runlevel2中暂时禁止该服务

update-rc.d <service name> default [NN | SS KK]


sudo update-rc.d rinetd default 80 80 

# default 表示在2 3 4 5 中添加80(the first 80)顺序的Start,在0 6 中添加80(the second 80)顺序的Kill服务

#从启动里面删除

sudo update-rc.d -f <service name> remove

#这样在所有的运行级别中就会删除掉关于该service的自启和关闭服务链接(删除的仅仅是链接,而不是/etc/init.d/文件夹中的脚本文件)

启动脚本的创建

现在我们以安装nginx为例,假设我们的nginx安装在/usr/sbin/目录下。

- 首先,我们在/etc/init.d创建文件sudo vim nginx,并将shell代码拷贝进去。

- 修改文件权限为755 sudo chmod 755 nginx。

- 添加服务到启动过程 sudo update-rc.d nginx defaults。


现在,我们就可以用以下命令正常启动nginx了。


# 启动

/etc/init.d/nginx start

# 停止

/etc/init.d/nginx stop

# 重启

/etc/init.d/nginx restart

引用

[1]关于Ubuntu运行级别、开机启动脚本的说明


[2]Ubuntu 添加删除开机启动项


推荐

  • QQ空间

  • 新浪微博

  • 人人网

  • 豆瓣

取消