How to enable the Startup Program in Linux

Source: Internet
Author: User

Linux Startup Program details

We assume that you are familiar with the boot process of other operating systems and understand the self-check boot steps of the hardware, starting with the boot loader of the Linux operating system (usually Lilo for personal computers, this section describes how to boot Linux.

Load the kernel
After lilo is started, if you choose Linux as the operating system for boot, the first thing to be loaded is the kernel. Remember that there is no operating system in the computer memory at this time, and PC (because of their natural design flaws) has no way to access all the memory on the machine. Therefore, the kernel must be fully loaded into the first MB of available Ram. For this purpose, the kernel is compressed. The header of this file contains the necessary code. First set the CPU to enter safe mode (to lift the memory limit), and then extract the remaining part of the kernel.

Run the kernel
After the kernel is decompressed in the memory, it can start to run. At this time, the kernel only knows its built-in functions, that is, the kernel part compiled as a module cannot be used. Basically, the kernel must have enough code to set its own virtual memory subsystem and root file system (usually ext2 File System ). Once the kernel starts running, the hardware detection determines which device drivers need to be initialized. From here on, the kernel can mount the root file system (this process is similar to Windows's process of identifying and accessing the C drive ). After the root file system is mounted to the kernel, a program called init will be started and run.

Note: here we intentionally omitted many details about Linux kernel startup, which are of interest only to kernel developers. If you're curious, visit the "kernel hackers Guide" at http://www.redhat.com: 8080 ".
INIT process
The INIT process is the first non-kernel process to be started, so its process id pid value is always 1. Init reads its configuration file/etc/inittab and determines the runlevel to be started ). Basically, the running level specifies the behavior of the entire system. Each level (represented by an integer ranging from 0 to 6) meets a specific purpose. If the initdefault level is defined, this value is directly selected. Otherwise, you must enter a value that represents the running level.
After entering a number indicating the running level, init executes a command script program according to the definition in the/etc/inittab file. The default running level depends on the selection of Logon programs in the installation phase: whether to use text-based or X-window-based logon programs.
We already know the RC command script program. When the running level changes, the/etc/inittab file defines which command script program to run. These Command Script programs start or stop various services at the specific running level. Because the number of services to be managed is large, you need to use the RC command script program. Among them, the most important one is/etc/rc. d/RC, which is responsible for calling the corresponding command script program for each running level in the correct order. As we can imagine, such a command script program can easily become difficult to control! A well-designed plan is required to prevent such incidents.

For each running level, there is a corresponding sub-directory in the/etc/rc. d sub-directory. The sub-directories at the running level are named as rcX. D. X indicates the number at the running level. For example, all the Command Script programs of running level 3 are saved in the/etc/rc. d/rc3.d subdirectory. In the sub-directories of each running level, the/etc/rc is created. d/init. d sub-directory command script program symbolic links, however, these symbolic links do not use the command script program in/etc/rc. d/init. the original name in the D sub-directory. If the command script program is used to start a service, its symbolic link name starts with the letter S. If the command script program is used to close a service, the name of the symbolic link starts with K.
In many cases, the execution sequence of these script programs is very important. If you do not configure the network interface first, you cannot use DNS to resolve the host name! In order to arrange their execution sequence, a two-digit character is followed by the letter S or K, and a small value is followed by a large value. For example,/etc/rc. d/rc3.d/s50inet will be executed before/etc/rc. d/rc3.d/s55named (s50inet configures network settings and 55named starts the DNS server ). Stored in/etc/rc. d/init. the Command Script programs in the D sub-directory and on the symbolic link are real practitioners who have completed the operations to start or stop various services. When/etc/rc. d/RC runs through each specific sub-directory of the running level, it calls each command script program for execution in order of numbers. It first runs the command script program with the letter K headers, and then runs the command script program with the letter S headers. The stop parameter is passed for the command script program that starts with letters K. Similarly, the start parameter is passed for the command script program that starts with letters S. Writing your own RC Command Script will certainly encounter the need for the system administrator to start or shut down the command
The current modification.

There are two ways to achieve the purpose of modification:
● If the modification only takes effect during boot, and the modification is not significant, you can simply edit the/etc/rc. d/rc. Local script. This command script program is executed at the last step of the boot process.
● If the modifications are meticulous or you want to shut down the process to make it stop running explicitly, you need to add a command script program in the/etc/rc. d/init. d subdirectory. The script program must accept the start and stop parameters and complete corresponding operations.
The first method is to edit the/etc/rc. d/rc. Local script. Of course, the two methods are relatively simple. If you want to add content to this command script program, you only need to open it using your favorite editor program, and then attach the command to be executed to the end of the file. This is indeed very convenient for the modification of one or two rows.

If you need to use a command script program, you must select the second method. Writing an RC Script is not as difficult as you think. Here is an example to see how it is implemented (by the way, you can use our example as a template to modify and add as needed ).
Suppose you want to call a special program every 60 minutes to bring up a message and remind you to leave the keyboard for a while. The command script contains the following parts:
● Description of the functions of the script program (so that it will not be forgotten one year later );
● Verify that the script program exists before trying to run it;
● Accept the start and stop parameters and perform the required actions.

After the parameter is specified, We can compile the script program of the command. This program is very simple. You can write it yourself. I will not give it here.
After writing a new command script program, add the necessary symbolic links to the relevant running level sub-directories to control the startup or stop of the script program. In my impression, I only want to make it start in runlevel 3 or runlevel 5 because I think only these two runlevel is the place where daily work is done. Finally, we hope the script program will be closed when it enters the running level 6 (restart.

When activating or disabling a service project, you may find that a specific service is not required to be started during the pilot process. This is especially true if you are considering replacing Windows NT files and print servers with Linux. We already know that you can change the Symbolic Link name in a specific sub-directory of the running level to prevent the service from being started. For example, you can change the first letter of its name from S to K. Once you have mastered the command line and symbolic links, you will find that this is the fastest way to activate or disable services.

When learning this renaming method, you may feel that the graphic operation interface ksysv is easier to grasp. Although it was originally designed and used in the KDE environment, it also runs well in the default GNOME environment installed in red hatlinux 7.2. To start it, simply open an xterm window and enter the ksysv command. A window appears, listing all the parameters that can be modified and online help is also needed. Warning: If you are learning the knowledge of this article in a real system, you need to use more common sense. When you try to modify the startup script program, remember that the modifications may make your system unable to work normally and cannot be restored using the restart method. Do not experiment with new settings on a running system. Back up all the files you want to modify. The most important thing is to prepare a boot disk at hand to prevent unexpected events.

 

Http://blog.csdn.net/xitong2012/article/details/7260956

 

 

In Linux, how does one enable a program automatically when it is started?

 

Core tips:System services can generally be automatically started at startup. What should I do if I want a program to be automatically started at startup in Linux? We know that you can set a shortcut in "start"> "All Programs"> "start" in windows. What about Linux ?... System services can generally be automatically started at startup. What should I do if I want a program to be automatically started at startup in Linux? We know that in windows, just set a shortcut in "start"> "All Programs"> "start". What about Linux?
 
This is also a simple problem. There are many ways to solve it. here we will introduce three methods. Because it is a simple introduction, the details are not very detailed. You can refer to man to see the relevant manual.
 
I./etc/rc. Local
This is the easiest way to edit "/etc/rc. local, enter the shell command of the Startup Program (the full path of the command to be entered), similar to "start" in windows ".

Use commandsVI/Etc/rc. Local

Add the full path of the program to be executed in the last line of the file.

For example, you need to run a Haha command every time you start the system. sh, put this script under/OPT, then you can go to "/etc/rc. add a line "/opt /. /haha. sh, or two "CD/OPT" and ". /haha. SH ".
 
Ii. crontab (similar to the Windows Task Scheduling Service)
You can use crontab to set the program execution schedule, for example, to run the program at every day or at every Monday.
Crontab-L: list the schedules;
Crontab-E: edit the schedule;
Crontab-D: delete a schedule;
 
There is nothing to say about "-l". It's just a look;
"-E" is editing, which is no different from VI (in fact, it is to use VI to edit a specific file );
"-D" is basically not used because it deletes all the user's timelines. Generally, "-e" is used to edit and delete the unwanted timelines line by line;
 
So how should we edit it?
 
The format of the crontab file is m h d m d cmd.
The last CMD is the program to be executed, such as Haha. Sh.
M: minute (0-59)
H: hour (0-23)
D: Date (1-31)
M: Month (1-12)
D: one day in a week (0-6, 0 represents Sunday)
 
These five time fields are separated by spaces. The value can be a number or multiple numbers (or others) separated by commas (,). If you do not need to set them, the default value is "*".
 
For example, run Haha. Sh at 08:05 every day, that is, "5 8 ***/opt/./Haha. Sh ".
 
It seems that it is far from "Automatic startup of the boot program". Now we are back to the question. In fact, the crontab function described above already has the ability to start automatically at startup. You can write a monitoring script and execute it every 5 minutes (*/5 ****. /haha. SH). If the program is absent, restart it once.
 
3. Register System Services
Services provided by the operating system, such as SSH and FTP, are automatically started upon startup. We can also use this method to increase the value of our programs ".

 

For example, to add a installed Service as a system service, run the following command:

Chkconfig -- add service name(First, add the service as a system service. Note that there are two horizontal bars in front of Add)

 

Chkconfig-Leve startup-level service name on

(Description: Level 3 indicates starting in command line mode, Level 5 indicates starting in graphic interface, and level on indicates enabling)

 

Chkconfig-Leve startup-level service name off

(Description: Off indicates disabling auto-start)

 

For example:Chkconfig-Level 3 MySQL on(Note: Enable MySQL service in command line mode and start with the system)

You can also use chkconfig -- add service name to delete system services.

**************************************** **************************************** **********

To view which services are added as system services, run the following command:

NtsysvOrChkconfig -- list

 

To view which programs are added as self-starting, run the following command:

CAT/etc/rc. Local(View the program paths added to this file)

**************************************** **************************************** ***********

 

The following is an example of how to add a shell script as a system service and start it with the system:

We can see that there are a lot of files under "/etc/rc. d/init. d", and each file can see the content, which is actually a shell script.
The system service is started through the script file in "/etc/rc. d/init. d. You can also write your own script here.
The content of the script file is also very simple, similar to this (for example, the name is "hahad "):
./Etc/init. d/functions
Start (){
Echo "starting my process"
CD/OPT
./Haha. Sh
}
Stop (){
Killall Haha. Sh
Echo "stoped"
}
After the script file is written, complete the following steps:
Chmod + x hahad # Add execution permission
Chkconfig -- add hahad # Add hahad to the System Service list
Chkconfig hahad on # Set the hahad switch (ON/OFF)
Chkconfig -- list hahad # You can see that the hahad service has been registered.

 
This completes all the work.

 

Article from: http://www.pckezhan.com/Html? 1009. html

 

Related Article

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.