linux-ubuntu与Debian系统中的update-rc.d(创建/注册系统服务)

来源:互联网 发布:mac上好用的播放器字幕 编辑:程序博客网 时间:2024/03/29 01:07

在ubuntu下开机自启动程序流程:

1.编写服务脚本放在/etc/init.d下,例如servicetest
/etc/init.d/servicectest
2.在/etc/rc*.d中制作相关的link。K开头是kill, S开头是start, 数字顺序代表启动的顺序。
update-rc.d 可以帮你的忙。
例如:
在/etc/init.d 中建立一个叫作servicetest的script , 然后
update-rc.d servicetest defaults

就会产生以下链結:

Adding system startup for /etc/init.d/servicetest ...
/etc/rc0.d/K20servicetest -> ../init.d/servicetest
/etc/rc1.d/K20servicetest -> ../init.d/servicetest
/etc/rc6.d/K20servicetest -> ../init.d/servicetest
/etc/rc2.d/S20servicetest -> ../init.d/servicetest
/etc/rc3.d/S20servicetest -> ../init.d/servicetest
/etc/rc4.d/S20servicetest -> ../init.d/servicetest
/etc/rc5.d/S20servicetest -> ../init.d/servicetest


=========华丽丽的分割线 呵呵============

基本知识之update-rc.d

update-rc.d命令,是用来自动的升级System V类型初始化脚本,
简单的讲就是,哪些东西是你想要系统在引导初始化的时候运行的,哪些是希望在关机或重启时停止的,可以用它来帮你设置。
这些脚本的连接位于/etc/rc*.d/LnName,对应脚本位于/etc/init.d/Script-name.
ubuntu与Debian 的update-rc.d与RH的chkconfig工具相类似。
然而chkconfig是一个二进制程序,而update-rc.d是一个Perl脚本。
这些工具有不同的命令行选项,但是却执行类似的功能。


1.添加服务
[1]
# sudo update-rc.d apache2 defaults
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K20apache2 -> ../init.d/apache2
/etc/rc1.d/K20apache2 -> ../init.d/apache2
/etc/rc6.d/K20apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2
[2]
# sudo update-rc.d apache2 defaults 91
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K91apache2 -> ../init.d/apache2
/etc/rc1.d/K91apache2 -> ../init.d/apache2
/etc/rc6.d/K91apache2 -> ../init.d/apache2
/etc/rc2.d/S91apache2 -> ../init.d/apache2
/etc/rc3.d/S91apache2 -> ../init.d/apache2
/etc/rc4.d/S91apache2 -> ../init.d/apache2
/etc/rc5.d/S91apache2 -> ../init.d/apache2
[3]
#sudo update-rc.d apache2 defaults 20 80
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2
[4]
# sudo update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S20apache2 -> ../init.d/apache2
[5]
# sudo update-rc.d apache2 start 20 2 3 4 . start 30 5 . stop 80 0 1 6 .
Adding system startup for /etc/init.d/apache2 ...
/etc/rc0.d/K80apache2 -> ../init.d/apache2
/etc/rc1.d/K80apache2 -> ../init.d/apache2
/etc/rc6.d/K80apache2 -> ../init.d/apache2
/etc/rc2.d/S20apache2 -> ../init.d/apache2
/etc/rc3.d/S20apache2 -> ../init.d/apache2
/etc/rc4.d/S20apache2 -> ../init.d/apache2
/etc/rc5.d/S30apache2 -> ../init.d/apache2

例如:
update-rc.d script-name start 90 1 2 3 4 5 . stop 52 0 6 .
start 90 1 2 3 4 5 . : 表示在1、2、3、4、5这五个运行级别中,按先后顺序,由小到大,第90个开始运行这个脚本。
stop 52 0 6 . :表示在0、6这两个运行级别中,按照先后顺序,由小到大,第52个停止这个脚本的运行。


2.删除服务
# sudo update-rc.d 服务名 remove
update-rc.d removes any links in the /etc/rcrunlevel.d directories to the script /etc/init.d/name.
The script  must  have  been deleted already - update-rc.d checks for this.
# sudo -f update-rc.d 服务名 remove      
-f     Force removal of symlinks even if /etc/init.d/name still exists
# sudo update-rc.d 服务名 stop 80 0 1 2 3 4 5 6 .
This command will only disable the service until next time the service is upgraded.
If you want to make sure the service won't be re-enabled upon upgrade, you should also type the above.

例如:
update-rc.d -f foobar remove
update-rc.d foobar stop 20 2 3 4 5 .

3.临时重启服务:
/etc/init.d/服务名 restart
4.临时关闭服务:
/etc/init.d/服务名 stop
5.临时启动服务:
/etc/init.d/服务名 start


FILES
       /etc/init.d/
              The directory containing the actual init scripts.

       /etc/rc*.d/
              The directories containing the links used by init and managed by
              update-rc.d.

       /etc/init.d/skeleton
              Model for use by writers of init.d scripts.

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lizzydarcymsp/archive/2010/06/03/5644598.aspx

 

 

 

 

------------------------------------------------------------------------------------------------------------

附加:

 

Ubuntu或者Debian系统中update-rc.d命令,是用来更新系统启动项的脚本。这些脚本的链接位于/etc/rcN.d/目录,对应脚本位于/etc/init.d/目录。在了解update-rc.d命令之前,你需要知道的是有关Linux 系统主要启动步骤,以及Ubuntu中运行级别的知识。

一、Linux 系统主要启动步骤
读取 MBR 的信息,启动 Boot Manager。


加载系统内核,启动 init 进程, init 进程是 Linux 的根进程,所有的系统进程都是它的子进程。
init 进程读取 /etc/inittab 文件中的信息,并进入预设的运行级别。通常情况下 /etc/rcS.d/ 目录下的启动脚本首先被执行,然后是/etc/rcN.d/ 目录。
根据 /etc/rcS.d/ 文件夹中对应的脚本启动 Xwindow 服务器 xorg,Xwindow 为 Linux 下的图形用户界面系统。
启动登录管理器,等待用户登录。
二、运行级别

 

Ubuntu中的运行级别

0(关闭系统)
1(单用户模式,只允许root用户对系统进行维护。)
2 到 5(多用户模式,其中3为字符界面,5为图形界面。)
6(重启系统)
切换运行级别
init [0123456Ss]
例如:init 0 命令关机; init 6 命令重新启动
启动项管理工具
sudo install sysv-rc-conf //或者使用带gui的工具bum
sudo sysv-rc-conf

三、update-rc.d命令详解
从所有的运行级别中删除指定启动项
update-rc.d -f <basename> remove
按指定顺序、在指定运行级别中启动或关闭
update-rc.d <basename> start|stop <order> <runlevels>
实例:update-rc.d apachectl start 20 2 3 4 5 . stop 20 0 1 6 .
解析:表示在2、3、4、5这五个运行级别中,由小到大,第20个开始运行apachectl;在 0 1 6这3个运行级别中,第20个关闭apachectl。这是合并起来的写法,注意它有2个点号,效果等于下面方法:

update-rc.d apachectl defaults
A启动后B才能启动,B关闭后A才关闭
update-rc.d A defaults 80 20
update-rc.d B defaults 90 10
启动和关闭顺序为90,级别默认
update-rc.d <basename> defaults 90

原创粉丝点击