Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:linux:init

启动流程

Linux 的运行级别 runlevel

LSB(Linux Standard Base) 4.1.0 specification:

ID Name Description
0 Halt Shuts down the system.
1 Single-User Mode Mode for administrative tasks.
2 Multi-User Mode Does not configure network interfaces and does not export networks services.
3 Multi-User Mode with Networking Starts the system normally.
4 Not used/User-definable For special purposes.
5 Start the system normally with appropriate display manager. ( with GUI ) As runlevel 3 + display manager.
6 Reboot Reboots the system.
  • S 在很多发行版中作为 Single-User Mode
  • 某些发行版中(如 DEBIAN), 2-5 分得不清

Runlevel

System-V

update-rc.d

install and remove System-V style init script links

Adding a service

Default priorities
$ update-rc.d apache2 defaults
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K20apache2 -> ../init.d/apache2
/etc/rc1.d/K20apache2 -> ../init.d/apache2
/etc/rc6.d/K20apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2 
Custom priorities
# a S20 link is started before a S91 and and K91 is kill before K20
$ update-rc.d apache2 defaults 91
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K91apache2 -> ../init.d/apache2
/etc/rc1.d/K91apache2 -> ../init.d/apache2
/etc/rc6.d/K91apache2 -> ../init.d/apache2
/etc/rc2.d/S91apache2 -> ../init.d/apache2
/etc/rc3.d/S91apache2 -> ../init.d/apache2
/etc/rc4.d/S91apache2 -> ../init.d/apache2
/etc/rc5.d/S91apache2 -> ../init.d/apache2 
Different priorities for Start and Kill
# update-rc.d apache2 defaults 20 80
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2

Specifying custom runlevels

# update-rc.d apache2 start 20 2 3 4 . start 30 5 . stop 80 0 1 6 .
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S30apache2 -> ../init.d/apache2

Removing a Service

$ update-rc.d -f apache2 remove

This command will only disable the service until next time the service is upgraded. If you want to make sure the service won't be re-enabled upon upgrade, you should also type the following:

update-rc.d apache2 stop 0 1 2 3 4 5 6 .

stop 没有的话, 就用 disable

LSBInitScripts header and inserv

/etc/init.d 中的脚本建议添加 LSBInitScripts header, 形如:

### BEGIN INIT INFO
# Provides:          scriptname
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO

但要注意:

  • 只添加 header 时脚本是不会自动按规则 start/stop 的, 需要用 insserv, 但 insserv 在 UBT 12.04 中不正常;
  • 所以最后还得用 update-rc.d, 而 update-rc.d scriptname defaults 时, 不读 header!

FILES

  • /etc/init.d/: The directory containing the actual init scripts.
  • /etc/rc?.d/: The directories containing the links used by init and managed by update-rc.d.
  • /etc/init.d/skeleton: Model for use by writers of init.d scripts.

参考

upstart

至今最好用服务管理脚本, 详见: upstart

MBR, GRUB & /boot

单用户模式

  1. 重启计算机,在 grub 的启动菜单显示时,用上下光标键中断自动启动
  2. 移动到所需要的启动菜单上,按 e 编辑该菜单项
  3. 找到 kernel 所在的行(linux /boot/vmlinuz-*)
  4. 在这一行的最后加上一个单词:single,C-x 结束编辑并 boot
it/linux/init.txt · Last modified: 2014/05/14 21:29 by admin