linux system init
2007-04-06 17:54
417 查看
linux system init (by hanlray(at)gmail.com)
unix类的系统通常都有多个runlevel,一个runlevel是一组服务的运行状态的配置:在该runlevel下哪些服务应该运行,哪些服务应该停止。一个服务的启动、停止等动作是由一个init script(通常包括在该服务程序的package中)控制的,LSB对init script做了标准化,一个标准的init script应该支持如下action:start start the service stop stop the service restart stop and restart the service if the service is already running, otherwise start the service try-restart restart the service if the service is already running reload cause the configuration of the service to be reloaded without actually stopping and restarting the service force-reload cause the configuration to be reloaded if the service supports this, otherwise restart the service if it is running status print the current status of the service
每个runlevel都对应一个目录,里面是一些到这些init script的symbol link,形如SNNxxx,KNNxxx,NN是一个序号,当改变到某个runlevel时(包括系统启动进入一个runlevel时),对该runlevel目录下的symbol links,先按序号顺序执行K打头的,执行的action为stop,再按序号顺序执行S打头的,执行的action为start。
手工管理各个runlevel是一件费力的事,LSB可以简化这一工作,其原理是在init script里写入该服务的meta信息:它依赖的服务、在哪些runlevel下启动等,然后由工具利用这些信息自动配置各个runlevel。下面是为xvnc写的一个init script(/etc/init.d/vncserver):
#! /bin/sh # # Author: Willem van der Mark <wvdmark@computer.org> # # /etc/init.d/xvncserver this Script # /usr/sbin/rcvncserver Root-Link to this Script # /usr/X11R6/bin/vncserver Program # # System startup script for vnc server # # LSB compatible service control script; see http://www.linuxbase.org/spec/ # # Note: This template uses functions rc_XXX defined in /etc/rc.status on # UnitedLinux (UL) based Linux distributions. If you want to base your # script on this template and ensure that it works on non UL based LSB # compliant Linux distributions, you either have to provide the rc.status # functions from UL or change the script to work without them. # # ### BEGIN INIT INFO # Provides: xvncserver # Required-Start: $remote_fs $syslog $network xdm # X-UnitedLinux-Should-Start: $network xdm # Required-Stop: $remote_fs $syslog $network xdm # X-UnitedLinux-Should-Stop: $network xdm # Default-Start: 3 5 # Default-Stop: 0 1 2 6 # Description: Start vncserver for remote control # ### END INIT INFO # Check for missing binaries VNC_WRAPPER=/usr/X11R6/bin/vncserver test -x $VNC_WRAPPER || exit 5 VNC_MASTER=/usr/X11R6/bin/Xvnc test -x $VNC_MASTER || exit 5 VNC_CONFIG=/etc/sysconfig/vncservers test -r $VNC_CONFIG || exit 5 # Shell functions sourced from /etc/rc.status: # rc_check check and set local and overall rc status # rc_status check and set local and overall rc status # rc_status -v ditto but be verbose in local rc status # rc_status -v -r ditto and clear the local rc status # rc_failed set local and overall rc status to failed # rc_failed <num> set local and overall rc status to <num><num> # rc_reset clear local rc status (overall remains) # rc_exit exit appropriate to overall rc status # rc_active checks whether a service is activated by symlinks . /etc/rc.status # First reset status of this service rc_reset # Return values acc. to LSB for all commands but status: # 0 - success # 1 - generic or unspecified error # 2 - invalid or excess argument(s) # 3 - unimplemented feature (e.g. "reload") # 4 - insufficient privilege # 5 - program is not installed # 6 - program is not configured # 7 - program is not running # VNCSERVERS="" VNCARGS="" [ -f $VNC_CONFIG ] && . $VNC_CONFIG prog="VNC server" case "$1" in start) for display in ${VNCSERVERS} do if test -f /home/${display##*:}/.vnc/passwd ; then rm -fv /tmp/.X${display%%:*}-lock rm -fv /tmp/.X11-unix/X${display%%:*} echo -n "Starting $prog on: ${display} -- " su ${display##*:} -l -c "cd && vncserver :${display%%:*} ${VNCARGS}" [ $? -eq 0 ] && echo $rc_done_up || echo $rc_failed_up else echo -n "Vnc not initialised for user: ${display##*:}" echo $rc_failed ; fi done ;; stop) for display in ${VNCSERVERS} do echo -n "Shutting down $prog: ${display} " su ${display##*:} -c "vncserver -kill :${display%%:*}" >/dev/null 2>&1 [ $? -eq 0 ] && echo $rc_done || echo $rc_failed done ;; restart) $0 stop $0 start rc_status ;; status) echo -n "Checking for service Vnc-Server " ## Check status with checkproc(8), if process is running ## checkproc will return with exit status 0. # Return value is slightly different for the status command: # 0 - service running # 1 - service dead, but /var/run/ pid file exists # 2 - service dead, but /var/lock/ lock file exists # 3 - service not running # NOTE: checkproc returns LSB compliant status values. checkproc $VNC_MASTER rc_status -v ;; *) echo "Usage: $0 {start|stop|status|restart}" exit 1 ;; esac rc_exit
# BEGIN INIT INFO和### END INIT INFO之间的文本就是该服务的meta信息,用install_initd/remove_initd来activate/deactivate符合LSB标准的init script:
/usr/lib/lsb/install_initd /etc/init.d/vncserver
install_initd根据这些meta信息在相应的runlevel目录下生成symbol link,并保证其依赖关系。
相关文章推荐
- Linux 启动顺序 添加开机启动服务 rc?.d rc.local init.d System run levels and init.d scripts
- Linux Init and System Initialization
- (OK) init: Service exec 1 (/system/bin/logwrapper) does not have a SELinux domain defined.
- linux自动启动shell和init概述(fedora use systemmd now!!!)
- Linux启动流程解析:init_post函数
- What is meaning of run level in Linux system? What are they?
- Linux 安全新闻: Linux 3.13、SystemRescueCD 4和BackBox 3.13
- Linux驱动late_initcall和module_init相关分析
- Linux init命令详解
- linux /etc/rc.d/init.d自启动程序说明
- Linux Filesystem Hierarchy Standard
- init.d,rc.d详解 Linux运行时详解
- linux下oracle11g sys和system忘记密码
- How Does The Linux File System Work?
- 浅析 Linux 初始化 init 系统: Systemd(2)
- 浅析 Linux 初始化 init 系统: sysvinit
- Linux file system events with C, Python and Ruby
- Linux SystemV ipc 实现 .
- Linux挂载U盘:mount: unknown filesystem type 'ntfs'
- Linux 启动详解之init