The English version of this article is from: http://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/
1. About /etc/init.d If you've ever used a Linux system, then you've heard of the init.d directory. What exactly is this catalog for? It does only one thing at the end of the day, but it is not a trivial thing, it is done for the whole system, so it is very important. The init.d directory contains many start and stop scripts for various services of the system. It controls everything from acpid to x11-common. Of course, init.d is far from that simple. (Translator's note: ACPID is a new power management standard for Linux operating systems; X11 is also called X Window system, X Window system (X11 or X) is a window system with bitmap display. It is a standard toolkit and protocol for building graphical user interfaces on Unix and Unix-like operating systems, as well as OpenVMS, and can be used for almost all modern operating systems). When you look at the /etc directory, you will find many directories that exist in the form of rc#.d (where # represents a specified initialization level, the range is 0~6). Under these directories, there are many scripts that control the process. These scripts either start with a "K" or an "S". Scripts that start with K run before scripts that start with S. Where these scripts are placed will determine when they start running. Between these directories, system services work together like a healthy machine. However, sometimes you want to start or kill a process cleanly without using the kill or killall commands. That's where /etc/init.d comes in handy! If you are using a Fedora system, you can find this directory: /etc/rc.d/init.d. Actually it does the same thing no matter where you put init.d. In order to be able to use scripts in the init.d directory, you need root or sudo privileges. Each script will be run as a command, which is structured roughly as follows: /etc/init.d/command option comand is the actual command that runs, and the options can be as follows: start stop reload restart force-reload In most cases, you'll use the start, stop, restart options. For example, if you want to turn off the network, you can use a command like this:
/etc/init.d/networking stop Another example is that you change your network settings and need to restart your network. You can use commands like this: /etc/init.d/networking restart The commonly used initialization scripts in the init.d directory are:
networking samba apache2 ftpd sshd dovecot mysql Of course, you may have other more commonly used scripts, depending on what Linux operating system you have installed. 2. About /etc/rc.local rc.local is also a script I use a lot. The script is executed after the system initialization-level script has been run, so it's safe to add the script you want to execute after the system boots up. A common case is that you can add an NFS mount/mount script to it. You can also add some script commands for debugging inside. For example, I have encountered this situation where the samba service always fails to function properly, and the inspection shows that samba should have started and executed during system startup, that is, the samba daemon configuration ensures that this function should be performed correctly. In this case, I usually don't bother to spend a lot of time looking why, I just need to add this line to the /etc/rc.local script: /etc/init.d/samba start In this way, the problem of SAMBA service exception was successfully solved.
3. Summary Linux is flexible. Because of its flexibility, we can always find many different ways to solve the same problem. The example of starting a system service is a good example. With the script in the /etc/init.d directory, coupled with the powerful tool of /etc/rc.local, you can rest assured that your service will be up and running flawlessly.
|