Linux Add Boot Script example

Source: Internet
Author: User
Tags stop script

Source: Linux Add Boot script example detailed

Linux (with Redhat as the template) to add boot scripts there are two methods, first to simple;

First, add in the/etc/rc.local
If you don't want to glue the script, or create a link or something,
The
Step1. First, modify the script so that all its modules can be executed when any directory is started;
Step2. Add a row at the end of/etc/rc.local to start the script with an absolute path;
Such as:
$ vim/etc/rc.local
#!/bin/sh
#
# This script is executed *after* all and the other init scripts.
# can put your own initialization stuff in here if you don ' t
# want to does the full Sys V style init stuff.

Touch/var/lock/subsys/local
. /etc/rc.d/rc.tune
/opt/pjt_test/test.pl

Save and exit;
Restart the test, then after the other programs are started, the script will be started;


Ii. advanced Boot-up script that can set the start priority
1. See if the service is self-booting
View the current status of a service
#service ServiceName status


View the start-up status of a service
#chkconfig--list ServiceName


2. Write a script, such as MySQL, and put it under/ETC/INIT.D to modify the properties:
$ sudo chmod x mysql

3. Create a symbolic connection:
$ cd/etc

Here are a few directories worth noting:
RCS.D rc0.d rc1.d ... rc6.d,
(0,1 ... 6 for Start Level 0 for stop, 1 for single user mode, 2-5 for multi-user mode, 6 for restart


Their role is equivalent to the RC.D under the Redhat,
You can put the script in the RCS.D, then modify the file name, give it a boot sequence number,
Such as:
S88mysql.


But the best way to do this is to put it under the appropriate boot level.
Specific practices:
Step1: Put MySQL in the/ETC/INIT.D directory first


Step2: View the boot level of the current system
$ sudo runlevel
N 3


Step3: Setting the start level
$ update-rc.d MySQL start 98 2.
98 is the start sequence number,
2 is the operating level of the system, can adjust itself,
Be careful not to forget the end of the period.


Now we go to the/etc/rc2.d, there is a s98mysql such a symbolic link.


4. Then start the system and test to see if the settings are valid.


Three, remove the symbolic link
When you need to remove this symbolic connection, there are three ways:
1. It is not the best way to delete the link directly under/ETC/RC2.D
2. $ update-rc.d-f S10 Remove//Recommended Practice
3. If UPDATE-RC.D command you are unfamiliar, you can also try rcconf This command, also very convenient.

Iv. Quotations
Source: http://hi.baidu.com/hosewen/blog/item/e77e27dbf4da4d68d0164e8e.html
Linux boot Auto-start scripting method
1. The starting and executing order of the Redhat is:
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).
All start with kxx, are called by the stop parameter;
All that begin with SXX are invoked with the start argument.


The order of calls is performed by XX from small to large. (where xx is a number, indicating the boot 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 operating modes 2, 3, and 5 in Redhat have/etc/rc.d/rc.local as the last of the Init scripts,
So the user can add in this file themselves some commands that need to be executed before the other initialization work.


Init waits for/ETC/RC.D/RC to finish executing
(Because the action/ETC/RC.D/RC in/etc/inittab 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
2.1 What is init:
Init is one of the most indispensable programs in Linux system operation.
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 the/bin/sh,
If the run fails, the startup of the system will fail.


2.2 Run Level
So, what exactly is a 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.
The different run-level definitions are as follows:
# 0-Stop (never set the Initdefault to 0)
# 1-Single user mode # s init s = init 1
# 2-Multiuser, no NFS
# 3-Full multi-user mode (standard run level)
# 4-No use
# 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. Chkconfig command (under Redhat OS)
Unlike DOS or Windows,linux, there can be multiple run levels. Common is the 2,3,4,5 of multiple users,
Many people know that 5 is the level of running X-windows, and 0 is the shutdown.
Changes to the run level can be toggled with the init command.


For example
Assuming you want to maintain the system into a single-user state, you can use init to switch.
During the switching process of Linux running level, the system will automatically look for files that start with K and S under the directory/ETC/RC[0-6].D of the run level.
These scripts are executed in the following numerical order.
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


The chkconfig has five functions:
Add services, delete services, list services, change startup information, and check 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, returns false.
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, 5 by default,
However, 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


4. Example Description:
Apache services are installed under Linux (by downloading binaries compiled for installation, not RPM packages), Apache service startup commands:
$/server/apache/bin/apachectl Start


Let the Apache service run below RunLevel 3.
The command is as follows:

$ touch/etc/rc.d/init.d/apache
$ vim/etc/rc.d/init.d/apache
Contents of Apache:
#!/bin/bash
#Start httpd Service
/server/apache/bin/apachectl start


$ chown-r Root/etc/rc.d/init.d/apache
$ chmod 700/etc/rc.d/init.d/apache
$ ll/etc/rc.d/rc3.d/
View the boot order of all services under this level;


$ ln-s/etc/rc.d/init.d/apache/etc/rc.d/rc3.d/s60apache
S is the shorthand of start, the representative starts;
K is the shorthand for kill, the representative closes;
60 numbers represent the order in which they are started. After 60 is usually the more insured number, since other services were started;


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)

Linux Add Boot Script example

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.