Get To Know Linux: The /etc/init.d Directory

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:網絡

  • start
  • stop
  • reload
  • restart
  • force-reload

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:

  • networking
  • samba
  • apache2
  • ftpd
  • sshd
  • dovecot
  • mysql

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
 
 

ABOUT JACK WALLEN

Jack has been a technical writer, covering Linux and open source, for nearly ten years. He began as an editor in chief of Linux content with Techrepublic and is now a freelance writer for numerous sites. Jack is also a writer of novels and is currently working on his first zombie fiction!
 
 
 
 
 

/etc/init.d

  若是你使用過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
  • reload
  • resart
  • force-reload

大多數狀況下你使用的是start,stop,restart。例如:你想關掉你的網路鏈接,你可使用以下的命令:
/etc/init.d/networking restart
或者當你修改了你的網絡配置,須要重啓網絡的時候。你可使用下面的命令:
/etc/init.d/networking restart

這個目錄下經常使用的腳本有:
- samba
- apache2
- ftpd
- sshd
- dovecot
- mysql
- networking
固然你的目錄下可能含有其餘的一些經常使用腳本。這個取決於你安裝了什麼服務。

/etc/rc.local

  /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/

相關文章
相關標籤/搜索