1.etc/init.d
一般的linux系统中,目录如下/etc/init.d,在fedora中是 /etc/rc.d/init.d,两者同义;一般fedora下也会直接建立软连接 /etc/init.d -> /etc/rc.d/init.d来兼容其他linux版本((⊙o⊙)…好像说的有点奇怪)
init.d只做了一件事情:控制许多系统各种服务的启动和停止脚本。它控制着所有从acpid到x11-common的各种事务。
当你查看/etc目录时,你会发现许多rc#.d 形式存在的目录(这里#代表一个指定的初始化级别,范围是0~6)。在这些目录之下,包含了许多对进程进行控制的脚本。这些脚本要么以"K"开头,要么以"S"开头。以K开头的脚本运行在以S开头的脚本之前。这些脚本放置的地方,将决定这些脚本什么时候开始运行。在这些目录之间,系统服务一起合作,就像运行状况良好的机器一样。然而,有时候你希望能在不使用kill 或killall 命令的情况下,能干净的启动或杀死一个进程。这就是/etc/init.d能够派上用场的地方了!
为了能够使用init.d目录下的脚本,你需要有root权限或sudo权限。每个脚本都将被作为一个命令运行,该命令的结构大致如下所示:
/etc/init.d/command 选项
command是实际运行的命令,选项可以有如下几种:
- start
- stop
- reload
- restart
- force-reload
大多数的情况下,你会使用start,stop,restart选项。例如,如果你想关闭网络,你可以使用如下形式的命令:
/etc/init.d/networking stop
又比如,你改变了网络设置,并且需要重启网络。你可以使用如下命令:
/etc/init.d/networking restart
init.d目录下常用初始化脚本有:
- networking
- samba
- apache2
- ftpd
- sshd
- dovecot
- mysql
当然,你可能有其他更多常用的脚本,这个取决于你安装了什么linux操作系统。
2.service
service命令和init.d/服务 作用一样,那么到底有什么区别呢
/etc/init.d
scripts are the old way of doing things. They come from the System V standard. However, those scripts are fired only in a particular sequence, no real dependencies can be established.
Therefore, upstart has been developed with the intent to substitute all the /etc/init.d
scripts with upstart scripts (in /etc/init
).
service
allows the smooth transition from /etc/init.d
scripts to upstart scripts. When in the future more on more scripts are transferred to upstart, service will still work, because it finds both possibilties.
http://askubuntu.com/questions/2075/whats-the-difference-between-service-and-etc-init-d
http://askubuntu.com/questions/19320/how-to-enable-or-disable-services
3./etc/rc.local
rc.local也是我经常使用的一个脚本。该脚本是在系统初始化级别脚本运行之后再执行的,因此可以安全地在里面添加你想在系统启动之后执行的脚本。常见的情况是你可以再里面添加nfs挂载/mount脚本。此外,你也可以在里面添加一些调试用的脚本命令。例如,我就碰到过这种情况:samba服务总是无法正常运行,而检查发现,samba是在系统启动过程中就该启动执行的,也就是说,samba守护程序配置保证了这种功能本应该正确执行。碰到这种类似情况,一般我也懒得花大量时间去查为什么,我只需要简单的在/etc/rc.local脚本里加上这么一行:
/etc/init.d/samba start
这样就成功的解决了samba服务异常的问题。
Linux是灵活的。正因为它的灵活性,我们总是可以找到许多不同的办法来解决同一个问题。启动系统服务的例子就是一个很好的佐证。有了/etc/init.d目录下的脚本,再加上/etc/rc.local这个利器,你可以放心的确保你的服务可以完美的启动和运行。