/etc/rc.local and/ETC/INIT.D Linux boot auto-run program

Source: Internet
Author: User


This is the user to customize the boot program, the need to start the automatic operation of the program written in this script
--------Reference----------------------
After completing the Run Level 3 service startup, if I have other actions I want to complete, for example, I also want to send a mail to a system management account, notify him, the system has just restarted, then, whether should be to create a shell script placed in/ETC/RC. D/init.d/inside, and then connected to the/etc/rc.d/rc3.d/inside? Oh! Of course you don't! Remember the/etc/rc.d/rc.local you mentioned in the last section? This file will execute the system commands you want to execute. Like Autoexec.bat and Config.sys in the early DOS era? ^_^
In other words, if I have any work that I want to do at boot time, I will write it directly to/etc/rc.local, then the work will be loaded automatically when the boot time! Instead of waiting for us to log into the system to start it! Is it convenient Ah! In general, Brother Bird likes to write his shell script full file name to/etc/rc.d/rc.local, so that the boot will be my shell script executed, it is really good!
--------Reference----------------------
That is, in the final phase of Linux startup, the system executes the commands stored in rc.local. It's a bit like the Start menu inside windows, but it's more cumbersome and more powerful.

Use case:

1) Startup of servers such as Oracle: If you need your Oracle to start with the system, you can look at the/etc/oratab file and then get to know it, and you'll find it's the right place to be.

2) Static routing: for the need to add a lot of not this network segment of the route, many people like to add similar in the/etc/rc.d/rc.local

Route add-net 192.168.1.0 netmask 255.255.255.0 GW 10.224.0.251

Route add-host 192.168.100.1 GW 10.224.0.251

2./etc/init.d
This directory contains scripts that are typically startup scripts for some of the services that Linux has set up to install with RPM packages. The system is installed in a lot of RPM packages, where there are many corresponding scripts. Executing these scripts can be used to start, stop, and restart these services. For example, if you want to restart SendMail, and your sendmail is installed in rpm, then release/etc/rc.d/init.d/sendmail restart to start SendMail directly!
As mentioned earlier, the/ETC/RC.D/INIT.D script in this directory is similar to the registry in Windows, which executes when the system starts. The program runs here (the Init process reads the RunLevel), is the script in the run INIT.D, but it is not run directly, but is optional because the system does not need to start all services.
So, how does the system choose which needs to start and what not? At this point, the level of operation that was just said worked. After the system-initiated run level is determined, the/ETC/RC.D/RC script executes first. In the source code of RH9 and FC7 it is a check_runlevel (although the implementation of the code is not the same, but also the same), after knowing the RunLevel, for each runlevel, under the RC.D have a subdirectory is rc0.d,rc1.d .... Rc6.d. Each directory is a subset of the scripts to the INIT.D directory for some links. Each level to perform which services in the corresponding directory, such as Level 5 to start the service is placed under RC5.D, but placed under this rc5.d are some link files, link to init.d in the corresponding file, really work to INIT.D script.

The following is the author's own version of the translation, if you want to reprint to other sites outside CSDN, please indicate this article link.

First, about/ETC/INIT.D

If you have used a Linux system, you must have heard of the INIT.D directory. What the hell is this directory for? It's only done one thing in the end, but it's very important that it's done for the whole system. The INIT.D directory contains many system startup and stop scripts for various services. It controls all kinds of transactions from Acpid to X11-common. Of course, INIT.D is far from being so simple. (Translator Note: Acpid is a new power management standard for Linux operating system; X11 is also called the X Window System, and the X Window System (X11 or X) is a bitmap-display windowing system. It is a standard toolkit and protocol for creating graphical user interfaces on Unix and UNIX-like operating systems, as well as OpenVMS, and can be used in almost any modern operating system.

When you look at the/etc directory, you will find many directories that exist in rc#.d form (where # represents a specified initialization level, and the range is 0~6). Under these directories, there are many scripts that control the process. These scripts either start with "K" or start with "S". A script starting with K runs before a script that begins with S. Where these scripts are placed, will determine when these scripts start running. Between these directories, system services work together, just like a well-functioning machine. However, sometimes you want to be able to start or kill a process cleanly without using the kill or Killall command. This is where/etc/init.d can come in handy!

If you are using the Fedora system, you can find this directory:/etc/rc.d/init.d. In fact, no matter where the INIT.D is placed, it plays the same role.

In order to be able to use scripts under the INIT.D directory, you need to have root or sudo permissions. Each script will be run as a command with a structure that looks similar to the following:

/etc/init.d/command Options

Comand is a command that is actually running, and the options can be as follows:

    • Start
    • Stop
    • Reload
    • Restart
    • Force-reload

Most of the cases, you will use the Start,stop,restart option. For example, if you want to turn off the network, you can use the following form of command:

/etc/init.d/networking stop

Another example is that you change the network settings and need to restart the network. You can use the following command:

/etc/init.d/networking restart

Common init scripts under the INIT.D directory are:

    • Networking
    • Samba
    • Apache2
    • Ftpd
    • Sshd
    • Dovecot
    • Mysql

Of course, you may have more commonly used scripts, depending on what Linux operating system you have installed.

Second, about/etc/rc.local

Rc.local is also a script that I often use. The script is executed after the system initialization level script runs, so you can safely add scripts that you want to execute after the system starts. The common scenario is that you can add NFS mount/mount scripts inside again. In addition, you can also add some debugging script commands. For example, I've come across a situation where samba services always fail to work, and the check finds that Samba is executing during system startup, which means that the Samba daemon configuration ensures that this functionality should be performed correctly. In this similar situation, I usually do not bother to spend a lot of time to find out why, I just need to simply add this line in the/etc/rc.local script:

/etc/init.d/samba start

This successfully solves the problem of samba service anomalies.

Iii. Summary

Linux is flexible. Because of its flexibility, we can always find many different ways to solve the same problem. An example of starting a system service is a good corroboration. With the script in the/ETC/INIT.D directory, plus the/etc/rc.local, you can be assured that your service will be up and running perfectly.

/etc/rc.local and/ETC/INIT.D Linux boot auto-run program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.