Linux system Init

Source: Internet
Author: User
Linux System init (by Hanlray (at) gmail.com)

UNIX-class systems typically have multiple runlevel, and a runlevel is the configuration of the running state of a set of services: which services should be running under that runlevel, and which services should be stopped. The start, stop, and so on of a service are controlled by an init script (usually included in the package of the service program), the LSB standardizes init script, and a standard init script should support the following action:

Start	start the service stop	Stop the service restart	stop and restart the service if the service is already r  Unning, otherwise start the service Try-restart Restart the service if is already running reload cause	The configuration of the service to be reloaded without actually stopping and restarting the service Force-reload	Cau Se the configuration to being reloaded if the service supports this, otherwise restart the service if it is running status	
Each runlevel corresponds to a directory in which some symbol link to these init script, like Snnxxx,knnxxx,nn, is an ordinal, when it changes to a runlevel (including when the system starts to enter a runlevel), For symbol links in the RunLevel directory, first executes k in ordinal order, the execution action is stop, and then the s begins in ordinal order, and the execution action is start.

It is laborious to manage each runlevel manually, and the LSB simplifies this by writing the meta information for the service in the Init script: the services It relies on, which runlevel to start, and so on, The tool then uses this information to automatically configure individual runlevel. The following is an init script (/etc/init.d/vncserver) written for Xvnc:

#! /bin/sh # Author:willem van der Mark <wvdmark@computer.org> #/etc/init.d/xvncserver this Script #/USR/SBIN/R Cvncserver Root-link to the script #/usr/x11r6/bin/vncserver program # System startup script for VNC server # # LSB C Ompatible Service Control script; http://www.linuxbase.org/spec/# Note:this template uses functions rc_xxx defined in/etc/rc.status on # Unitedlinu X (UL) based Linux distributions. If you are want to base your # script on this template and ensure the It works on non UL based LSB # compliant Linux Distribu tions, you are either have to provide the Rc.status # functions from UL or change the script to work without. # ### BEGIN INIT INFO # provides:xvncserver # required-start: $remote _fs $syslog $network XDm # X-unitedlinux-sho Uld-start: $network XDm # required-stop: $remote _fs $syslog $network XDm # x-unitedlinux-should-stop: $network XDm # Defa Ult-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 is verbose in the 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 CH Ecks whether a service is activated by symlinks. /etc/rc.status # I reset status of this service Rc_reset # return values acc. to LSBFor all commands but status: # 0-success # 1-generic or unspecified error # 2-invalid or excess argument (s) # 3-un Implemented feature (e.g. "Reload") # 4-insufficient Privilege # 5-program is not installed # 6-program are not confi Gured # 7-program is not running # vncservers= "vncargs=" "[F $VNC _config] &&. $VNC _config prog= "VNC server" case "' 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 && Vncserve R: ${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} does 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) $ stop $ start rc_status;; Status) echo-n "Checking for Service Vnc-server" # # Check Status with Checkproc (8), if process is running # # check  		Proc'll return with exit status 0.  # return value are 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 re 		Turns LSB compliant status values.     Checkproc $VNC _master rc_status-v;; *) echo "Usage: $ {Start|stop|status|restart}" exit 1;;  Esac Rc_exit
# The text between the BEGIN init info and ### end INIT info is the meta information for the service, using INSTALL_INITD/REMOVE_INITD to activate/deactivate the LSB compliant INIT Script

/usr/lib/lsb/install_initd/etc/init.d/vncserver
Based on these meta information, INSTALL_INITD generates symbol link under the corresponding RunLevel directory and guarantees its dependencies.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.