Linux boot Auto-start scripting method

Source: Internet
Author: User
Tags stop script

through the site of the Tianjin IPTV demo Project commissioning, IPTV This system has a better understanding and understanding, because the IPTV itself needs to install a lot of services, and on-site implementation of these need to start the services of the script added to the Open Machine automatic operation, if the server restart after the need to manually open a lot of services, work and later maintenance is relatively cumbersome, specifically summed up under the Linux boot automatic startup script involved in the knowledge and methods, as follows:

1, relevant basic knowledge points
1) The starting mode and execution order of Redhat are:
Load kernel
Executing the INIT program
/etc/rc.d/rc.sysinit # The first script executed by Init
/ETC/RC.D/RC $RUNLEVEL # $RUNLEVEL is the default operating mode
/etc/rc.d/rc.local #相应级别服务启动之后, execute the file (you can actually write the command you want to execute in the file)
/sbin/mingetty # Waiting for user to log in

In Redhat,/etc/rc.d/rc.sysinit primarily does the same initialization work in each of the operating modes, including:
Incoming Keymap and system fonts
Start swapping
Set host name
Setting up NIS domain names
Check (fsck) and Mount file system
Open Quota
Loading the sound card module
Setting the system clock
Wait a minute.
/ETC/RC.D/RC executes the script under the corresponding directory according to the run mode specified by its parameters (run level, which you can set in the Inittab file). Those who start with kxx
are called by the Stop parameter, and all start with sxx are invoked with the start argument. Called in order by XX
From small to large to execute. (where xx is a number, which is the start order) For example, assuming that the default operating mode is 3,/ETC/RC.D/RC, it will be called as described above
The script under/etc/rc.d/rc3.d/.
It is worth mentioning that the Redhat in the operating mode 2, 3, 5 are the/etc/rc.d/rc.local as the initialization of the script
The last one, so the user can add in this file himself some commands that need to be executed before the other initialization work, after logging in.

Init waits for the/ETC/RC.D/RC to finish executing (because the/ETC/RC.D/RC in/etc/inittab
Action is wait), the/sbin/mingetty is run on the specified virtual terminal, waiting for the user to log on.
At this point, Linux starts to end.
2) init runlevel and instructions
     One, what is Init:init is one of the indispensable programs in the operation of Linux system.  The so-called Init process, which is a user-level process initiated by the kernel. After the kernel has booted itself (already loaded into memory, started running, and has initialized all device drivers and data structures, etc.), the boot process is completed by starting a user-level program init.  As a result, Init is always the first process (its process number is always 1). The kernel will find it in several places in the past that used Init, and its correct location (for Linux systems) is/sbin/init.  If the kernel cannot find Init, it will try to run/bin/sh, and if it fails, the boot of the system will fail.  Second, operational level so, what is the run level? Simply put, the run level is the functional level that the operating system is currently running.  This level is from 1 to 6 and has different functions. Different run level definitions are as follows # 0-Downtime (never set Initdefault to 0) # 1-Single user mode           &nbsp ;                           # s   init s = init 1 # 2-multiuser, no NFS # 3-Full multi-user mode (standard shipping Line level) # 4-No use for # 5-x11 multi-user graphics mode (xwindow) # 6-reboot (never set Initdefault to 6) These levels are specified in the/etc/inittab file. This file is the main file that the INIT program looks for, and the first service to run is the file that is placed in the/ETC/RC.D directory. In most Linux distributions, startup scripts are located in/etc/rc.d/init.d. These scripts are connected to the/ETC/RC.D/RCN.D directory with the ln command. (n here is the run-level 0-6)      3): ChkconfiThe G command (under the Redhat OS)       Unlike DOS or windows,linux can have multiple run levels. Common is the multi-user 2,3,4,5, many people know that 5 is the level of running X-windows, and 0 on        is shutdown. Changes to the run level can be toggled with the init command. For example, if you want to maintain the system into a single-user state, you can use init to switch. During the switching process of the        linux, the system automatically looks for files that begin with the/etc/rc[0-6].d of K and S under the directory of the run level, in the following order of numbers, Execute these        scripts. The maintenance of these scripts is a tedious thing, Linux provides the chkconfig command to update and query system services at different run levels.
The syntax is:

Chkconfig--list [Name]
Chkconfig--add Name
Chkconfig--del Name
Chkconfig [--level levels] Name
Chkconfig [--level levels] Name

Chkconfig has five features: adding services, deleting services, listing services, changing startup information, and checking the startup status of a particular service.

Chkconfig displays usage when no parameters are run. If the service name is added, check that the service is started at the current run level. If yes, returns TRUE, otherwise false is returned. The--level option allows you to specify the run level you want to view, not necessarily the current run level.

If On,off or reset is specified after the service name, Chkconfig will change the startup information for the specified service. On and off refer to the start and stop of the service at the time of changing the operating level. Reset refers to initializing the service information, regardless of what is specified by the problematic initialization script.

For the on and off switches, the system is only valid for run Level 3, 4, and 5 by default, but reset can be valid for all run levels. When you specify the--level option, you can select a specific run level.

It is necessary to note that for each run level, there can be only one startup script or stop script. When you switch the run level, Init does not restart the service that is already started, nor does it stop the service that has stopped.

Option Description:

--level levels
Specifies the run level, a string consisting of numbers 0 through 7, such as:

--level 35 represents the specified run Level 3 and 5.
To deactivate the NFS service at run Level 3, 4, 5, use the following command: Chkconfig--level 345 NFS Off

--add Name
This option adds a new service, Chkconfig ensures that each run level has a start (S) or Kill (K) entry. If one is missing, it is automatically created from the default init script.

--del Name
Used to delete the service and remove the associated symbolic connection from/ETC/RC[0-6].D.

--list Name
List, if name is specified, then only the specified service name is displayed, otherwise the state of all services at different run levels is listed.

Run-level files

Each service managed by Chkconfig needs to add two or more lines of comments to the script under the corresponding INIT.D.
The first line tells Chkconfig the run level of the default startup and the priority of start and stop. If a service does not start at any run level by default, use-instead of run-level.
The second line describes the service and can be commented on across lines.

For example, Random.init contains three rows:
# chkconfig:2345 20 80
# description:saves and restores system entropy pool for
# higher quality random number generation.
Indicates that the random script should start at run Level 2, 3, 4, 5, start with a priority of 20, and stop priority 80.

Well, the introduction is here, to see your own directory under the/etc/rc.d/init.d of the script bar.

Set up self-starting service: Chkconfig--level 345 NFS on

2. Example Introduction: 1, installed under Linux Apache Service (by downloading binary files for economic compilation installation, not RPM package), Apache service start command:/server/apache/bin/apachectl start.  Let the Apache service run below RunLevel 3. The command is as follows:

1) Touch/etc/rc.d/init.d/apache
Vi/etc/rc.d/init.d/apache
Chown-r Root/etc/rc.d/init.d/apache
chmod 700/etc/rc.d/init.d/apache
Ln-s/etc/rc.d/init.d/apache/etc/rc.d/rc3.d/s60apache #S is the shorthand for start, the start, the K is the shorthand for kill, the representative is closed. 60 numbers represent the order in which they are started. (For IPTV systems, many services are built on the premise that the database starts, you can adjust the script's boot order by the number.)

Contents of Apache:
#!/bin/bash
#Start httpd Service
/server/apache/bin/apachectl start

At this point the Apache service can be automatically started at run Level 3 at random. (Can be combined with chkconfig to adjust the start-up service accordingly)

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.