Linux add power-on Self-starter script sample detailed __linux

Source: Internet
Author: User
Tags mysql in stop script

Http://blog.chinaunix.net/uid-26000296-id-3986914.html


Linux (Redhat as a template) to add a boot from the script there are two ways, first to simple;

First, add in the/etc/rc.local
If you don't want to stick the script, or create a link or something,
The
Step1. First, modify the script so that all of its modules can be in any directory startup normal execution;
Step2. Then add a row at the end of the/etc/rc.local to start the script in an absolute path;
Such as:
$ vim/etc/rc.local
#!/bin/sh
#
# This script would be executed *after* all the other init scripts.
# You can put your own initialization stuff with 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;
After the test is restarted, the script will start after the other programs are started.


Second, can set the boot priority level of advanced boot from the script
1. See if the service is starting
To view the current status of a service
#service ServiceName status


To view the startup automatic status of a service
#chkconfig--list ServiceName


2. Write a good script, such as MySQL, put it under/ETC/INIT.D, 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 multiuser mode, 6 for restart)


They are equivalent to the RC.D under Redhat,
You can put the script in the RCS.D, then change the filename, give it a start number,
Such as:
S88mysql.


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


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


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


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


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


Third, remove the symbolic link
When you need to remove this symbolic connection, there are three ways:
1. Directly to the/ETC/RC2.D to delete the corresponding link, of course, is not the best way
2. $ update-rc.d-f S10 Remove/Recommended Practice
3. If you are not familiar with the UPDATE-RC.D command, 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 Script 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 run mode
/etc/rc.d/rc.local #相应级别服务启动之后, executing the file (you can actually write the command you want to execute into the file)
/sbin/mingetty # Waiting for user login


In Redhat,/etc/rc.d/rc.sysinit primarily does the same initialization work in each of the running modes, including:
Dial in Keymap and system fonts
Start swapping
Set host name
Set NIS domain name
Check (fsck) and Mount file system
Open Quota
Load sound card module
Set the system clock
Wait a minute.


/ETC/RC.D/RC the running mode (run level, which you can set in the Inittab file) according to its parameters to execute the script in the corresponding directory.
All start with the KXX, all with stop as the parameter to call;
Those that start with sxx are called with start as arguments.


The order of the calls is performed in XX from small to large. (where xx is a number, indicating the boot order)
For example
Assuming the default run mode is 3,/ETC/RC.D/RC, it will be invoked as described above
/etc/rc.d/rc3.d/under the script.


It is worth mentioning that the running patterns in Redhat 2, 3, and 5/etc/rc.d/rc.local as the last of the initialization scripts,
So the user can add some commands in this file that need to be executed before they log on after other initialization work.


Init waits for the/ETC/RC.D/RC to finish executing
(Because the/ETC/RC.D/RC action in/etc/inittab is wait),
/sbin/mingetty will be run on each virtual terminal specified, waiting for the user to log on.
At this point, Linux startup is over.


2. Init operation level and instruction
2.1 What is init:
Init is one of the most indispensable programs in Linux system operations.
The so-called init process, which is a user-level process initiated by the kernel.
After the kernel starts itself (has been loaded into memory, started running, and has initialized all device drivers and data structures, etc.),
Completes the boot process by starting a user-level program init.
Therefore, Init is always the first process (its process number is always 1).


The kernel finds it in several places in the past that have 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,
If the operation fails, the boot of the system will also fail.


2.2 Run Level
So, what is the run level?
Simply put, the run level is the level of functionality that the operating system is currently running.
This level, from 1 to 6, has different functions.
The different run levels are defined as follows:
# 0-Downtime (never set the Initdefault to 0)
# 1-Single user mode # s init s = init 1
# 2-Multiuser, no NFS
# 3-Full multiuser mode (standard run level)
# 4-No use
# 5-x11 Multi-user graphics mode (xwindow)
# 6-Reboot (don't set the Initdefault to 6)


These levels are specified in the/etc/inittab file.
This file is the primary file that the INIT program is looking for, and the first service to run is the file 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 using the ln command. (Here's n is run level 0-6)


3. Chkconfig command (under Redhat operating system)
Unlike DOS or Windows,linux, you can have a variety of runtime levels. Common is the 2,3,4,5 of multi-user,
Many people know that 5 is the level of running X-windows, and 0 is shutdown.
Changes to the run level can be toggled through the init command.


For example
Suppose you want to maintain the system into Single-user state, then you can use init to switch.
In the Linux run-level switching process, the system will automatically look for the corresponding run-level directory/ETC/RC[0-6].D under the K and S, the file,
These scripts are executed in the following numerical order.
The maintenance of these scripts is a tedious task, and Linux provides the chkconfig command to update and query system services at different runtime 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 features:
Add services, delete services, list services, change startup information, and check the startup status of specific services.


Chkconfig when no parameters are running, display usage. If you add a service name, check to see if the service is starting at the current run level.
If yes, returns True,
Otherwise, returns false.
The--level option can specify the run level to view, not necessarily the current run level.


If On,off or reset is specified after the service name, chkconfig changes the startup information for the specified service.
On and off refer to the start and stop of a service when it changes the run level.
Reset refers to initializing the service information, regardless of what is specified by the problematic initialization script.
For on and off switches, the system defaults to run Level 3, 4, 5 only,
But reset can be valid for all run levels.
When you specify the--level option, you can select a specific run level.


It should be explained that there can be only one startup script or stop script for each run level.
When the run level is switched, Init does not restart the service that has started, nor does it stop the service that has stopped.


Introduction to Options:
--level levels
Specifies the run level, consisting of a string of digits 0 through 7, such as:
--level 35 indicates that run level 3 and 5 are specified.
To stop NFS services at run Level 3, 4, 5, use the following command: Chkconfig--level 345 NFS Off


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


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


--list Name
List that lists the status of all services at different run levels if name is specified and only the specified service name is displayed.


Run-level files
Each service managed by Chkconfig needs to be annotated with two or more lines of script under the corresponding INIT.D.
The first line tells Chkconfig the run level of the default boot and the priority of start and stop.
If a service does not start at any run level by default, then use-instead of run level.


The second line describes the service, and you can use a cross row annotation.
For example, Random.init contains three lines:
# 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, with a start priority of 20 and a stop priority of 80.
Well, the introduction is here, to see their own directory under the/ETC/RC.D/INIT.D under the script bar.
Setting up the self-service: Chkconfig--level 345 NFS on


4. Introduction to the example:
The Apache service is installed under Linux (compiled by downloading binaries, not rpm packages), and the Apache service startup command:
$/server/apache/bin/apachectl Start


Let the Apache service run under Run Level 3.
The order is as follows:

$ touch/etc/rc.d/init.d/apache
$ vim/etc/rc.d/init.d/apache
The 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 start order of all services under this level;


$ ln-s/etc/rc.d/init.d/apache/etc/rc.d/rc3.d/s60apache
S is the abbreviation of start, the representative starts;
K is the abbreviation of the kill, the representative closes;
60 numbers represent the order in which they are started. After 60 is usually the number of the comparison insurance, because the other services have been started;


The Apache service can then be automatically started at run Level 3 at random.
(Can be combined with chkconfig to start the service to adjust 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.