高级编程のlinux下shell编程技巧 运行级别介绍
本章详解
- 运行级别 inittab 启动应用程序
3.1 运行级别
- 运行级别目录(/etc/rcN.d)ubuntu下在目录/etc下
- 当前运行级别命令(runlevel)
- 运行级别目录文件格式(SXXscript,KXXscript)script表示脚本名字,S表示该运行级别被启动,XX表示数字,对应启动顺序,K表示不要被启动
3.2 inittab
- 对于redhat系统来讲,运行级别控制文件为:/etc/inittab
- 对于ubuntu系统来讲,运行级别控制文件为:/etc/init/rc-sysinit.conf
解释:在Ubuntu 6.1之后,开始用upstart替代init,主要脚本都在/etc/event.d下面,默认情况下,没有/etc/inittab文件。而运行控制级别文件为/etc/event.d/rc-default . 但是现在又用/etc/init取代了原来的/etc/event.d,运行控制级别文件为rc-sysinit.conf
内容为:
1 # rc-sysinit - System V initialisation compatibility 2 # 3 # This task runs the old System V-style system initialisation scripts, 4 # and enters the default runlevel when finished. 5 6 description "System V initialisation compatibility" 7 author "Scott James Remnant <scott@netsplit.com>" 8 9 start on (filesystem and static-network-up) or failsafe-boot 10 stop on runlevel 11 12 # Default runlevel, this may be overriden on the kernel command-line 13 # or by faking an old /etc/inittab entry 14 env DEFAULT_RUNLEVEL=2 15 16 emits runlevel 17 18 # There can be no previous runlevel here, but there might be old 19 # information in /var/run/utmp that we pick up, and we don't want 20 # that. 21 # 22 # These override that 23 env RUNLEVEL= 24 env PREVLEVEL= 25 26 console output 27 env INIT_VERBOSE 28 29 task 30 31 script 32 # Check for default runlevel in /etc/inittab 33 if [ -r /etc/inittab ] 34 then 35 eval "$(sed -nre 's/^[^#][^:]*:([0-6sS]):initdefault:.*/DEFAULT_RUNLEVEL="\1";/p' /etc/inittab || true)" 36 fi 37 38 # Check kernel command-line for typical arguments 39 for ARG in $(cat /proc/cmdline) 40 do 41 case "${ARG}" in 42 -b|emergency) 43 # Emergency shell 44 [ -n "${FROM_SINGLE_USER_MODE}" ] || sulogin 45 ;; 46 [0123456sS]) 47 # Override runlevel 48 DEFAULT_RUNLEVEL="${ARG}" 49 ;; 50 -s|single) 51 # Single user mode 52 [ -n "${FROM_SINGLE_USER_MODE}" ] || DEFAULT_RUNLEVEL=S 53 ;; 54 esac 55 done 56 57 # Run the system initialisation scripts 58 [ -n "${FROM_SINGLE_USER_MODE}" ] || /etc/init.d/rcS 59 60 # Switch into the default runlevel 61 telinit "${DEFAULT_RUNLEVEL}" 62 end script
它为使用/etc/inittab保留了入口。我们可以自己建立一个inittab,放在默认的路径/etc下面。在其中加入诸如默认启动级别之类的信息,如:
id:3:initdefault:
将自己写的启动脚本,放置到/etc/init.d目录下面,再对应的启动文件rcN.d下面建立该脚本的超连接即可!!!