Jade Dungeon

initd

运行级别

Linux操作系统在运行中,可以分为不同的级别:

0 系统停机状态
1 单用户或系统维护状态
2~5 多用户状态
6 重新启动
S  

切换运行级别:

init 6         # 重新启动

配置系统服务

配置文件位置在:/etc/init.d

建立启动服务

编写一个启动控制脚本,以proxy为例,建立/etc/init.d/proxy

#!/bin/sh
case "$1" in
	start)
		start-stop-daemon --start --background --exec /root/proxy.py
	;;
	stop)
		start-stop-daemon --stop --name proxy.py
esac

start-stop-daemon是一个可以管理daemon进程的程序:

  • start的时候,使用--exec指定要执行的文件
  • stop的时候,使用--name根据进程名字来使用killall结束匹配的进程。

控制服务的启动

使用service命令或者调用init.d下的脚本启动、关闭或者重启进程。例如:

service proxy start
service proxy stop 
service proxy restart

或者:

/etc/init.d/proxy start
/etc/init.d/proxy stop
/etc/init.d/proxy restart

自动启动服务

Linux开机的时候,不是直接运行/etc/init.d下的所有脚本的,而是根据不同的 runlevel来执行/etc/rc$runlevel.d下的脚本。这里的runlevel是用以区别系统的 运行方式(例如单用户的runlevel,多媒体桌面的runlevel,服务器的runlevel都不同)。

在Debian里,可以使用update-rc.d来把/etc/init.d/proxy安装到各个runlevel中。

update-rc.d <脚本> <安装方式> <启动顺序>

如:

update-rc.d proxy defaults 99

卸载随机启动的服务,执行:

update-rc.d -f proxy remove 

update-rc.d安装的时候提示了警告信息,是因为我们写的/etc/init.d/proxy 太简陋了,连LSB的信息也没有提供。

update-rc.d: warning: /etc/init.d/proxy missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>

只需要做一些小改动,就可以避免那个警告了。如下:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          proxy
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start or stop the HTTP Proxy.
### END INIT INFO

case "$1" in
	start)
		start-stop-daemon --start --background --exec /root/proxy.py
	;;
	stop)
		start-stop-daemon --stop --name proxy.py
esac

在指定运行级别启动服务

/etc/rc2.d该目录为runlevel=2环境(就是Ubuntu默认情况)的启动项:

qii@ubuntu:/etc/rc2.d$ ls
README             S20nginx              S20xinetd      S50rsync      S90binfmt-support  S99grub-common
S19postgresql-8.4  S20speech-dispatcher  S25bluetooth   S50saned      S91apache2         S99ondemand
S20fancontrol      S20wicd               S50cups        S70dns-clean  S91lighttpd        S99rc.local
S20kerneloops      S20winbind            S50pulseaudio  S70pppd-dns   S99acpi-support

其中S表示启动,随后的数字表示启动的顺序。

手动的话将S重命名为K,运行:

sudo update-rc.d script defaults

可以禁用某个服务,不过比较麻烦,方便的方法是sysv-rc-conf

查看/修改不同运行级别的启动项:sysv-rc-conf

查看/修改不同运行级别的启动项,可以发现Ubuntu(其实是Debian)2-5之间的 runlevel效果是一样的。

sudo apt-get install sysv-rc-conf 
sudo sysv-rc-conf 

以控制ntp这个服务的自动启动为例:

root@dlp:~# aptitude -y install sysv-rc-conf

root@dlp:~# sysv-rc-conf --list      # list services
root@dlp:~# sysv-rc-conf ntp on      # set auto-start for NTP
root@dlp:~# sysv-rc-conf ntp off     # disable auto-start for NTP

按指定顺序、运行级别:update-rc.d

按指定顺序、在指定运行级别中启动或关闭:

update-rc.d <basename> start|stop <order> <runlevels>

添加启动项,例如mysql

qii@ubuntu:/etc/rc2.d$ sudo update-rc.d mysql defaults 
update-rc.d: warning: /etc/init.d/mysql missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
 Adding system startup for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql -> ../init.d/mysql
   /etc/rc1.d/K20mysql -> ../init.d/mysql
   /etc/rc6.d/K20mysql -> ../init.d/mysql
   /etc/rc2.d/S20mysql -> ../init.d/mysql
   /etc/rc3.d/S20mysql -> ../init.d/mysql
   /etc/rc4.d/S20mysql -> ../init.d/mysql
   /etc/rc5.d/S20mysql -> ../init.d/mysql

删除启动项

qii@ubuntu:/etc/rc2.d$ sudo update-rc.d -f mysql remove 
 Removing any system startup links for /etc/init.d/mysql ...
   /etc/rc0.d/K20mysql
   /etc/rc1.d/K20mysql
   /etc/rc2.d/S20mysql
   /etc/rc3.d/S20mysql
   /etc/rc4.d/S20mysql
   /etc/rc5.d/S20mysql
   /etc/rc6.d/K20mysql

实例:

update-rc.d apachectl start 20 2 3 4 5 . stop 20 0 1 6 .

解析:

  • 在2、3、4、5这五个运行级别中,由小到大,第20个开始运行apachectl;
  • 在 0 1 6这3个运行级别中,第20个关闭apachectl。

这是合并起来的写法,注意它有2个点号,效果等于下面方法:

update-rc.d apachectl defaults

A启动后B才能启动,B关闭后A才关闭:

update-rc.d A defaults 80 20 update-rc.d B defaults 90 10

启动和关闭顺序为90,级别默认:

update-rc.d <basename> defaults 90

修改当前运行级别的启动项:rcconf

查看,修改当前运行级别的启动项

sudo apt-get install rcconf
sudo rcconf

随开机启动配置:rc.local

把命令写到/etc/rc.d/rc.local或者/etc/rc.local里,这样虽然能够实现随机运行, 但是并不够灵活。不能像mysql,apache等服务一样能够像使用service命令或者调用 init.d下的脚本启动、关闭或者重启进程。例如,

service mysql restart
service apache2 stop 

或者

/etc/init.d/mysql restart
/etc/init.d/apache2 stop

控制服务和任务:initctl

initctl也用来控制服务和任务。

列出服务

root@dlp:~# initctl list
mountnfs-bootclean.sh start/running
rsyslog start/running, process 689
tty4 start/running, process 1090
udev start/running, process 531
upstart-udev-bridge start/running, process 520
.....
.....

停止服务

以atd服务为例,如果要停止atd服务:

initctl stop atd

服务启动脚本

/etc/init目录下有每个任务的启动脚本。比如要不让atd服务自动启动, 就注释掉/etc/init/atd.conf中的这一行:

# comment out
# # start on runlevel [2345]

查找哪些服务会启动

root@dlp:~#
grep "start on" /etc/init/*

/etc/init/acpid.conf:start on runlevel [2345]
/etc/init/apport.conf:start on runlevel [2345]
/etc/init/atd.conf:#start on runlevel [2345]
/etc/init/bootmisc.sh.conf:start on virtual-filesystems
/etc/init/checkfs.sh.conf:start on mounted MOUNTPOINT=/
/etc/init/checkroot-bootclean.sh.conf:start on mounted MOUNTPOINT=/
/etc/init/checkroot.sh.conf:start on mounted MOUNTPOINT=/
/etc/init/console.conf:start on stopped rc RUNLEVEL=[2345] and container CONTAINER=lxc
/etc/init/console-font.conf:start on starting plymouth-splash
/etc/init/console-setup.conf:start on (virtual-filesystems
/etc/init/container-detect.conf:start on mounted MOUNTPOINT=/run
.....
.....

root@dlp:~#
man upstart-events

Table 1: Well-Known System Events Summary.

|-----------------------------------------------------------------------------|
|Ref |          Event           | Type | Emit |          Time          | Note |
|-----------------------------------------------------------------------------|
|    | all-swaps                |  S   |  M   | > (5)                  |      |
|    | control-alt-delete(7)    |  S   |  A   | > (5)                  |  A   |
|    | container                |  S   |  C   | > /run mounted         |  Q   |
|    | dbus-activation          |  S   |  B   | > D-Bus client request |      |
|    | deconfiguring-networking |  H   |  V   | < non-local IFs down   |  P   |
|    | desktop-session-start    |  H   |  D   | > X(7) session created |  B   |
|    | desktop-shutdown         |  H   |  D   | > X(7) session ended   |  O   |
|    | device-not-ready         |  H   |  M   | > (2)                  |  N   |
|    | drm-device-added         |  S   |  U   | > (5)                  |  C   |
|    | failsafe-boot            |  S   |  X   | > (7) and local IF     |  S   |
|    | file                     |  S   |  K   | > (1)                  |  U   |
| 7  | filesystem               |  S   |  M   | After last (1)         |  D   |
|    | graphics-device-added    |  S   |  U   | > (5)                  |  C   |
|    | keyboard-request(7)      |  S   |  A   | > (5)                  |  E   |
|    | local-filesystems(7)     |  S   |  M   | > (6)                  |      |
|    | login-session-start      |  H   |  D   | < DM running           |  F   |
| 1  | mounted(7)               |  H   |  M   | > associated (2)       |  G   |
| 2  | mounting(7)              |  H   |  M   | > (5)                  |  H   |
| 3  | net-device-added         |  S   |  U   | > (5)                  |  C   |
|    | net-device-changed       |  S   |  U   | > (5)                  |  C   |
|    | net-device-down          |  S   |  F   | < (4)                  |  C   |
| 4  | net-device-removed       |  S   |  U   | > (5)                  |  C   |
|    | net-device-up            |  S   | F,N  | > (3)                  |  C   |
|    | not-container            |  S   |  C   | > /run mounted         |  Q   |
|    | power-status-changed(7)  |  S   |  I   | > (5)                  |  I   |
|    | recovery                 |  S   |  G   | Boot (<5)              |  R   |
|    | remote-filesystems(7)    |  S   |  M   | > (6)                  |      |
|    | runlevel(7)              |  M   |  T   | > (7) + (8)            |      |
|    | socket(7)                |  S   |  S   | > socket connection    |      |
| 5  | startup(7)               |  S   |  I   | Boot                   |  J   |
|    | started(7)               |  S   |  I   | > job started          |  K   |
|    | starting(7)              |  H   |  I   | < job starts           |  K   |
| 8  | static-network-up        |  S   |  N   | > last static IF up    |      |
|    | stopped(7)               |  S   |  I   | > job stopped          |  K   |
|    | stopping(7)              |  H   |  I   | < job stops            |  K   |
|    | unmounted-remote-        |  H   |  V   | >   last   remote   FS |  L   |
|    | filesystems              |      |      | unmounted              |      |
| 6  | virtual-filesystems(7)   |  S   |  M   | > last virtual FS (1)  |  M   |
|-----------------------------------------------------------------------------|

Table 2: Well-Known User Events Summary.

|------------------------------------------------------------------------------|
|Ref |      Event       | Type | Emit |              Time               | Note |
|------------------------------------------------------------------------------|
|    | desktop-end(7)   |  S   |  J   | < (2)                           |      |
|    | desktop-start(7) |  H   |  J   | > (3)                           |      |
|    | file             |  S   |  K   | > (1)                           |  U   |
| 2  | session-end(7)   |  M   |  I   | < Session Init end              |      |
| 1  | startup(7)       |  S   |  I   | > Session Init start            |  J   |
|    | :sys:*           |  S   |  E   | > upstart-event-bridge(8) start |      |
| 3  | xsession         |  M   |  H   | > (1)                           |  T   |
|------------------------------------------------------------------------------|

Table 3: Event Types.

|-------------------------------------------------|
|Ref | Event Type | Notes                         |
|-------------------------------------------------|
| H  | Hook       | Blocking.  Waits  for  events |
|    |            | that start on or stop on this |
|    |            | event.                        |
| M  | Method     | Blocking task.                |
| S  | Signal     | Non-blocking.                 |
|-------------------------------------------------|

Table 4: Event Emitters.

|-------------------------------------------------------------------------|
|Ref | Emitter                          | Notes                           |
|-------------------------------------------------------------------------|
| A  | System Administrator (initiator) | Technically emitted by init(8). |
| B  | dbus-daemon(1)                   | Run with "--activation=upstart" |
| C  | container-detect job             |                                 |
| D  | Display Manager                  | e.g. lightdm/gdm/kdm/xdm.       |
| E  | upstart-event-bridge(8)          |                                 |
| F  | ifup(8) or ifdown(8)             | See /etc/network/.              |
| G  | bootloader or initramfs          |                                 |
| H  | xsession-init session job        |                                 |
| I  | init(8)                          | Either PID 1 or a Session Init. |
| J  | job that starts desktop          | gnome-session job for Ubuntu.   |
| K  | upstart-file-bridge(8)           |                                 |
| M  | mountall(8)                      |                                 |
| N  | network-interface job            |                                 |
| S  | upstart-socket-bridge(8)         |                                 |
| T  | telinit(8), shutdown(8)          |                                 |
| U  | upstart-udev-bridge(8)           |                                 |
| V  | System V init system             |                                 |
| X  | failsafe job                     |                                 |
|-------------------------------------------------------------------------|

Table 5: Event Summary Notes.

|-----------------------------------------------------------------------|
|Note | Detail                                                          |
|-----------------------------------------------------------------------|
| A   | Requires administrator to press Control-Alt-Delete key combina- |
|     | tion on the console.                                            |
| B   | Event generated when user performs graphical login.             |
| C   | These are specific examples. upstart-udev-bridge(8)  will  emit |
|     | events  which  match the pattern, "S-device-A" where 'S' is the |
|     | udev subsystem and 'A' is the udev action. See udev(7) and  for |
|     | further  details.  If  you  have sysfs mounted, you can look in |
|     | /sys/class/ for possible values for subsystem.                  |
| D   | Note this is in the singular - there is no 'filesystems' event. |
| E   | Emitted when administrator presses Alt-UpArrow key  combination |
|     | on the console.                                                 |
| F   | Denotes Display Manager running (about to be displayed), but no |
|     | users logged in yet.                                            |
| G   | Generated for each mount that completes successfully.           |
| H   | Emitted when mount attempt for single entry from  fstab(5)  for |
|     | any filesystem type is about to begin.                          |
| I   | Emitted when Upstart receives the SIGPWR signal.                |
| J   | Initial event (system or Session Init).                         |
| K   | Although the events are emmitted by init(8), the instigator may |
|     | be initctl(8) if a System Administrator has manually started or |
|     | stopped a job.                                                  |
| L   | /etc/init/umountnfs.sh.                                         |
| M   | Emitted when all virtual filesystems (such as /proc) mounted.   |
| N   | Emitted  when the --dev-wait-time timeout is exceeded for moun- |
|     | tall(8).  This defaults to 30 seconds.                          |
| O   | Emitted when the X(7) display  manager  exits  at  shutdown  or |
|     | reboot, to hand off to the shutdown splash manager.             |
| P   | Emitted  by  /etc/init.d/networking  just prior to stopping all |
|     | non-local network interfaces.                                   |
| Q   | Either 'container' or 'not-container' is emitted (depending  on |
|     | the environment), but not both.                                 |
| R   | Emitted  by  either  the  initramfs  or bootloader (for example |
|     | grub) as the initial event (rather than startup(7))  to  denote |
|     | the  system has booted into recovery mode. If recovery was suc- |
|     | cessful, the standard startup(7) event is then emitted,  allow- |
|     | ing the system to boot as normal.                               |
| S   | Emitted  to  indicate  the system has failed to boot within the |
|     | expected time. This event will trigger other jobs  to  forcibly |
|     | attempt to bring the system into a usable state.                |
| T   | Only emitted for a graphical session.                           |
| U   | See file-event(7).                                              |
|-----------------------------------------------------------------------|