Update-rc.d && rc.local管理Ubuntu的开机启动

前言

虽然是原创,毕竟自己对ubuntu的开机启动研究了整整两天,期间看鸟哥linux私房菜、研究/etc/init.d/下shell源码等等,但是不可否认看了很多国内国外的文章,最终搞定也是基于一篇不错的博客文章,这里首先进行推荐: 通过update-rc.d管理ubuntu开机启动
接下来,我会按照参考博客内容记录一些自己的收获(测试环境ubuntu10.04 && ubuntu12.04)

Ubuntu系统运行级别

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

update-rc.d概要

Linux services can be started, stopped and reload with the use of scripts stocked(贮存) in /etc/init.d/. However, during start up or when changing runlevel, those scripts are searched in /etc/rcX.d/ where X is the runlever number(可以用runlevel命令查看系统默认的runlevel). This tutorial will explain how one can activate(激活), disactivate or modifu a service start up. When installing a new service under debian, the default is to enable it. So for instance, if you just installed apache2 package, after you installed it, apache service will be started and  so will it be upon the next reboots. If you do not use apache all the time, you might want to disable this service from starting up upon boot up and simply start it  manually when you actually need it by running this command:
sudo  /etc/init.d/apache2 start

You could either disable this service on boot up by removing any symbolic links in /etc/rcX.d/SYYapache2 or by using update-rc.d.The advantage of using update-rc.d is that is will take care of removing/adding any required links to /etc/init.d automatically.Taking apache2 as an example.



As you can see, for runlevels 0, 1 and 6 there is a K at the begining of the link, for runlevels 2, 3,  4 and 5, there is a S. Those two letters stands for Kill and Start. 

Removing A Service

If you want to totally disable apache2 service by hand, you would need to delete every single link in /etc/rcX.d/. Using update-rc.d it is a simple as :

update-rc.d  [-f] servicename remove

Option  -f   Force removal of symlinks even if /etc/init.d/name still exists.(相当于就算/etc/init.d/下还存在apache脚本,但是/etc/rcX.d/下所有关于apache2的软链接均被删除)




Adding A Service

Default Priorities

Now, if you want to re-add your service to be started on boot up, you can simply use:



Specifying Custom Runlevels

Finally, if you want to Start and Kill on Specific runlevels, like for instances starting writelog with priority 90 on runlevels 2, 3, 4, 5 and kill with priority 91 on runlevels 0, 1 and 6, you can follows this operation:




我的一个init.d启动脚本样例

wangzhengyi@cloud-1:/etc/init.d$ cat writelog 
#!/bin/sh

### BEGIN INIT INFO
# Provides:          wzy
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: test update-rc.d to start a service
### END INIT INF

PATH="/sbin:/usr/sbin:/bin:/usr/bin"
DESC="write log daemon"
SCRIPTNAME="/home/wangzhengyi/scripts/ceshi.sh"


export PATH=$PATH

. /lib/lsb/init-functions

do_start() {
	log_begin_msg "Runing wzy writeing log scripts $SCRIPTNAME"
	if [ -x $SCRIPTNAME ]; then
		$SCRIPTNAME
		ES=$?
	fi
	log_end_msg $ES
}

case "$1" in 
	start)
		do_start
		;;
	restart|reload|force-reload|staus)
		echo "Error: argument '$1' not supported" >&2
	        exit 3
		;;
	stop)
		;;
	*)
        	echo "Usage: $0 start|stop" >&2
        	exit 3
        	;;
esac

	
exit 0

rc.local

首选,查看一下rc.local文件的运行权限和运行level



可以看出,rc.local运行在runlevel为2,3,4,5,并且运行优先级为99,也就是最低的优先级,因此我们也可以把需要开机启动的脚本加入到rc.local里面去

wangzhengyi@cloud-1:~$ cat /etc/rc.local 
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#开机运行测试脚本
if [ -x /home/wangzhengyi/scripts/ceshi.sh ]; then
	 /home/wangzhengyi/scripts/ceshi.sh
fi

exit 0

后记

开机启动不外乎这两种方法,我推荐第一种update-rc.d管理,因为这样灵活性更高,并可以解决相关依赖,这块研究了将近两天,把linux开机启动流程都学了一遍,收获还是不小的,希望对大家有帮助!

你可能感兴趣的:(架构设计,Ubuntu,运维,bash,shell)