Chinaunix首页 | 论坛 | 博客
  • 博客访问: 446564
  • 博文数量: 481
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1040
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-06 14:09
文章分类

全部博文(481)

文章存档

2013年(483)

我的朋友

分类: LINUX

2013-02-28 12:47:03

在Linux系统下,一个Services的启动、停止以及重启通常是通过/etc/init.d目录下的脚本来控制的。然而,在启动或改变运行级别时,是在/etc/rcX.d中来搜索脚本。其中X是运行级别的number。本文将解释如何启动、关闭和修改服务的运行。

当你在Debian下安装一个新的服务,比如Apache2,安装完成后,默认情况下它会启动,并在下一次重启后自动启动。

但是如果你不是一直需要这个服务,只在需要的时候启用它,你可以禁用它。直到你需要使用的时候,执行如下指令:

/etc/init.d/apache2 start

要实现这个目的,你需要先在/etc/rcX.d目录中删除所有apache2的符号链接,但这个方法操作麻烦,且效率低下。因此,我们建议你使用update-rc.d命令来实现这个功能。

1、删除一个服务

如果你想手动的完全禁用Apache2服务,你需要删除其中的所有在/etc/rcX.d中的单一链路。但是如果使用update-rc.d,则非常简单:

update-rc.d -f apache2 remove

参数-f是强制删除符号链接,即使/etc/init.d/apache2仍然存在。

Note:这个命令仅仅禁止该服务,直到该服务被升级。如果你想在服务升级后仍然保持被禁用。应该执行如下的命令:

update-rc.d apache2 stop 80 0 1 2 3 4 5 6 .

2、增加一个服务

如果你想重新添加这个服务并让它开机自动执行,你需要执行以下命令:

update-rc.d apache2 defaults

并且可以指定该服务的启动顺序:

update-rc.d apache2 defaults 90

还可以更详细的控制start与kill顺序:

update-rc.d apache2 defaults 20 80

其中前面的20是start时的运行顺序级别,80为kill时的级别。也可以写成:

update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .

其中0~6为运行级别。

update-rc.d命令不仅适用Linux服务,编写的脚本同样可以用这个命令设为开机自动运行。具体参见一文。





EXAMPLES
       Insert links using the defaults:
          update-rc.d foobar defaults
       The equivalent dependency header would have start and stop
       dependencies on $remote_fs and $syslog, and start in
       runlevels 2-5 and stop in runlevels 0, 1 and 6.
       Equivalent command using explicit argument sets:
          update-rc.d foobar start 20 2 3 4 5 . stop 20 0 1 6 .
       More typical command using explicit argument sets:
          update-rc.d foobar start 30 2 3 4 5 . stop 70 0 1 6 .
       Insert links at default runlevels when B requires A
          update-rc.d script_for_A defaults 80 20
          update-rc.d script_for_B defaults 90 10
       Insert a link to a service that (presumably) will not be needed by any other daemon
          update-rc.d top_level_app defaults 98 02
       Insert links for a script that requires services that start/stop at sequence number 20
          update-rc.d script_depends_on_svc20 defaults 21 19
       Remove all links for a script (assuming foobar has been deleted already):
          update-rc.d foobar remove
       Example of disabling a service:
          update-rc.d -f foobar remove
          update-rc.d foobar stop 20 2 3 4 5 .
       Example of a command for installing a system initialization-and-shutdown script:
          update-rc.d foobar start 45 S . stop 31 0 6 .
       Example of a command for disabling a system initialization-and-shutdown script:
          update-rc.d -f foobar remove
          update-rc.d foobar stop 45 S .
阅读(231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~