If you use Linux you most likely have heard of the init.d directory. But what exactly does this directory do? It ultimately does one thing but it does that one thing for your entire system, so init.d is very important. The init.d directory contains a number of start/stop scripts for various services on your system. Everything from acpid to x11-common is controlled from this directory. Of course it's not exactly that simple.mysql
If you look at the /etc directory you will find directories that are in the form rc#.d (Where # is a number reflects a specific initialization level - from 0 to 6). Within each of these directories is a number of other scripts that control processes. These scripts will either begin with a "K" or an "S". All "K" scripts are run before "S" scripts. And depending upon where the scripts are located will determine when the scripts initiate. Between the directories the system services work together like a well-oiled machine. But there are times when you need to start or stop a process cleanly and without using the kill or killall commands. That is where the /etc/init.d directory comes in handy.linux
Now if you are using a distribution like Fedora you might find this directory in /etc/rc.d/init.d. Regardless of location, it serves the same purpose.sql
In order to control any of the scripts in init.d manually you have to have root (or sudo) access. Each script will be run as a command and the structure of the command will look like:apache
/etc/init.d/command OPTIONc#
Where command is the actual command to run and OPTION can be one of the following:网络
Most often you will use either start, stop, or restart. So if you want to stop your network you can issue the command:less
/etc/init.d/networking stopssh
Or if you make a change to your network and need to restart it, you could do so with the following command:post
/etc/init.d/networking restartflex
Some of the more common init scripts in this directory are:
Of course there may be more often-used scripts in your directory - it depends upon what you have installed. The above list was taken from a Ubuntu Server 8.10 installation so a standard desktop installation would have a few less networking-type scripts.
But what about /etc/rc.local
There is a third option that I used to use quite a bit. This option is the /etc/rc.local script. This file runs after all other init level scripts have run, so it's safe to put various commands that you want to have issued upon startup. Many times I will place mounting instructions for things like nfs in this script. This is also a good place to place "troubleshooting" scripts in. For instance, once I had a machine that, for some reason, samba seemed to not want to start. Even afer checking to make sure the Samba daemon was setup to initialize at boot up. So instead of spending all of my time up front with this I simply placed the line:
/etc/init.d/samba start
in the /etc/rc.local script and Samba worked like a charm. Eventually I would come back and trouble shoot this issue.
Final Thoughts
Linux is flexible. Linux is so flexible there is almost, inevitably, numerous ways to solve a single problem. Starting a system service is one such issue. With the help of the /etc/init.d system (as well as /etc/rc.local) you can pretty much rest assured your service will start.
Advertisement 若是你使用过linux系统,那么你必定听过init.d目录。可是这个目录到底是作什么用的?他基本上只作一件事,可是这件事是为你的整个系统服务的,因此init.d目录很是重要。这个目录里面包含了一系列系统里面服务的开启和中止的脚本。从acpid到x11-common的各项事务都受它控制。因此init.d没有想象的那么简单。
当你查看/etc目录时,你会发现许多rc#.d形式的目录。(#表明数字0-6,指相对的初始化水平)这些目录中包含许多进程的控制脚本。这些脚本以’K’或者’S’开头。全部以’K’开头的脚本先于以’S’开头的脚本运行。这些脚本所在位置将决定这些脚本启动的时间。这些系统系统服务像一个高效率的机器同样在这些目录间共同工做。有时候你想在不使用kill或者killall进程的状况下,干净的启动或者中止一个脚本的时候,、inin.d这个目录就派上用场了。
若是你是用的是Fedore系统,你将能够找到/etc/rc.d/init.d目录。不管init.d在什么位置,它都将发挥一样的做用。
为了可以使用init.d目录下的脚本,你须要有root或者sudo权限。全部的这些命令将被当作一个命令运行,这些命令的结构大体以下:
/etc/init.d/command OPTION
command指的是你实际运行的命令。OPTION能够是下面几种:
大多数状况下你使用的是start,stop,restart。例如:你想关掉你的网路链接,你可使用以下的命令:
/etc/init.d/networking restart
或者当你修改了你的网络配置,须要重启网络的时候。你可使用下面的命令:
/etc/init.d/networking restart
这个目录下经常使用的脚本有:
- samba
- apache2
- ftpd
- sshd
- dovecot
- mysql
- networking
固然你的目录下可能含有其余的一些经常使用脚本。这个取决于你安装了什么服务。
/etc/rc.local是我常常是用的另外一个脚本。这个脚本在系统初始化脚本以后运行,因此你能够放心的将你想在系统启动后执行的脚本放在里面。一般我会将nfs的挂载脚本放在里面。同时也是一个放调试脚本的好地方。好比,有一次个人系统中得samba服务没法正常启动,尽管检查确认本该随着系统一块儿启动的。一般我也会话大量的时间去寻找缘由,仅仅是在rc.local文件里下写下这么一行
/etc/init.d/samba start
samba没法启动的问题就解决了
Linux系统是灵活的,正由于这种灵活性,同一个问题总能够找到多种解决方案。启动系统服务就是个很好的例子。 在/etc/init.d和/etc/rc.local的帮助下,你能够确保你的系统服务能够启动和运行。
https://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/