linux system init

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   # # /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   set local and overall rc status to  #      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,linux,system,service,network,wrapper,action)