In linux, almost all installed software won't be automatically started by default, and I need to start it manually. This is very bad for WEB servers, apache, and mysql, so how do we remember that these software is automatically started upon startup? Let's take a look.
Recently, the VPS is often unstable, and the VPS is easily stuck and there is no Swap space. It is automatically restarted after the crash. Mysql is not started when Centos is installed, so that the website cannot be accessed after each restart, you must manually connect to SSH to open Mysql for access. In this case, it may have been several times. I have never been concerned about the past few times, but I have been visiting it twice recently, I put Mysql into the startup item when it gets annoying. In fact, this is not very good, because if your Mysql settings cache is relatively large, when the server is started, the memory may be insufficient and it will be troublesome. I set the Mysql cache to a relatively small value, and there will be more than one hundred memory in the VPS after running, so I am not afraid.
Centos can be set to boot as follows:
Vi/etc/rc. d/rc. local Add the path of the program to be started on the last line:
| The Code is as follows: |
Copy code |
/Usr/sbin/apachectl start # apache server /Etc/rc. d/init. d/mysqld start # mysql server /Etc/rc. d/init. d/smb start # samba server /Usr/local/subversion/bin/svnserve-d # svn server |
The path is set with the installation.
Automatically run the program in Red Hat Linux
1. automatically run the program at startup
After Linux is loaded, it initializes the hardware and device drivers, and then runs the first process init. Init starts other processes based on the configuration file. Normally, the modification is placed in/etc/rc or/etc/rc. d or/etc/rc ?. The script file in the d directory can enable init to automatically start other programs. For example, to edit the/etc/rc. d/rc. local file, add "xinit" or "startx" at the end of the file. You can directly enter X-Window after the file is started.
2. automatically run the program upon Logon
When a user logs on, bash automatically runs the global logon script:/etc/profile created by the system administrator. Bash then searches for one of the three special files in sequence in the user's starting Directory:/. bash_profile,/. bash_login, And/. profile, but only executes the first one found.
Therefore, you only need to add commands to the above files as needed to automatically run certain programs (similar to Autoexec. bat in DOS) during user logon ).
3. automatically run the program upon logon.
When you log out, bash automatically runs the personal logout script/. bash_logout. For example /. add the command "tar-cvzf c. source. tgz *. c ", the system will automatically execute the" tar "command to back up every time you log out *. c file.
4. Periodically run the program automatically
Linux has a daemon called crond. Its main function is to periodically check the content of a group of command files in the/var/spool/cron directory, and execute the commands in these files at the specified time. You can use the crontab command to create, modify, and delete these command files.
For example, after you create a crondFile with the content "00 9 23 Jan * HappyBirthday" and run the "crontab cronFile" command, the system automatically runs the "HappyBirthday" Program ("*" indicates that no matter the day of the day is the day of the week) at AM on November 23, lunar January ).
5. automatically run the program once at a scheduled time
Scheduled command execution at is similar to crond (but it is only executed once): The command is executed at a given time, but is not automatically repeated. The at command is generally in the format of at [-f file] time. All commands in the file are executed at the specified time. You can also enter the command directly from the keyboard:
| The Code is as follows: |
Copy code |
$ At 12: 00 At> mailto Roger-s "Have a lunch" <plan.txt At> Ctr-D Job 1 An email titled "Have a lunch" is automatically sent to Roger at, containing the plan.txt file. #! /Bin/bash RESTART = "..." # Write the corresponding service code START = "......." STOP = ".........." Case "$1" in Restart) $ RESTART Echo "......" ;; Start) $ START Echo "......" ;; STOP) $ STOP Echo "......" ;; *) Echo "Usage: $0 {restart transaction start transaction stop }" Exit 1 Esac Exit 1 |
After writing the script, you need to modify the chmod u + x test. sh permission.
First, all the services started randomly in linux are in/etc/init. d. All the files in this folder are script files (in simple words, the script program writes the program to be run to a file so that the system can execute it in order, similar to windows autorun. in addition, there are folders such as rc1.d, rc2.d to rc6.d In the/etc folder, which are different runlevels in linux, generally, the running level of multiple users in X windows is 5th, that is, rc5.d. The script file in this folder is the service program to be started randomly when the script file is 5th. Note that in each rc (1-6 ). the files in the d folder are all/etc/init. A soft connection to the files in the d folder (similar to the shortcut in windows), that is, in/etc/init. the d folder contains all the service programs, and each rc (1-6 ). d. Only link it to start the desired service program!
To start scim (a program), we first need to know where the scim program is. We can use the locate command to find it. scim is in/usr/bin/scim, usr indicates that it belongs to the user, and bin indicates that the program can be executed in linux. In this way, I can write a script program, put it in/etc/init. d, and then make a corresponding soft link in rc5.d.
This script is actually very simple, just two lines:
| The Code is as follows: |
Copy code |
#! /Bin/bash /Usr/bin/scim |
The first line is to declare the terminal used to run the script, and the second line is the command to run.
Note that in rc5.d, the name of each link starts with S or K, K starts with a random start. In this way, you can know which service I want to start at random, and change the first letter K to S. Of course, after changing S to K, this service cannot be started randomly. Therefore, we need to name this link SXXX so that the system can start it randomly.
Add auto-start script
First, put your script in/etc/init. d, and then execute the following command:
Update-rc.d a start 90 2 3 4 5. stop 90 0 1 6.
A is your script. Note that there are two points.
A script example.
| The Code is as follows: |
Copy code |
#! /Bin/sh # Source function library. If [-f/etc/init. d/functions]; then ./Etc/init. d/functions Else ./Lib/lsb/init-functions Fi MOD =/a. ko Start () { Echo-n $ "insert a kernel module :" /Sbin/insmod $ MOD Echo } Stop () { Echo-n $ "remove a kernel module :" /Sbin/rmmod a-f Echo } [-F $ MOD] | exit 0 # See how we were called. Case "$1" in Start) Start ;; Stop) Stop ;; Restart | reload) Stop Start ;; *) Echo $ "Usage: $0 {start | stop | restart | reload }" |
The update-rc.d command is used to automatically upgrade the System V type initialization script, simply put, what is what you want the System to run during boot initialization, if you want to stop the instance when it is shut down or restarted, you can use it to help you set it. The connection of these scripts is located in/etc/rcn. d/LnName, and the corresponding Script is located in/etc/init. d/Script-name.
1. Set the startup sequence and running-level startup items:
Update-rc.d <service> start <order> <runlevels>
2. Set to stop at the specified running level in the specified order:
Update-rc.d <service> stop <order> <runlevels>
3. Delete the specified startup Item from all running levels:
Update-rc.d-f <script-name> remove
For example:
| The Code is as follows: |
Copy code |
Update-rc.d script-name start 90 1 2 3 4 5. stop 52 0 6.
|
Start 90 1 2 3 4 5.: indicates that the script starts to run in the order of 1, 2, 3, 4, and 5.
Stop 52 0 6.: indicates that the script is stopped from small to large in the order of 0 and 6.
If you add a script to/etc/init. d, you need to create the link
In/etc/rc *. d. K starts with kill, S starts with start, and numerical order indicates the start order. (SysV)
Update-rc.d can help you.
Example:
Create a script named zope in/etc/init. d, and then
| The Code is as follows: |
Copy code |
Update-rc.d zope defaults |
Which of the following links will be generated? ::
| The Code is as follows: |
Copy code |
Adding system startup for/etc/init. d/zope... /Etc/rc0.d/K20zope-> ../init. d/zope /Etc/rc1.d/K20zope-> ../init. d/zope /Etc/rc6.d/K20zope-> ../init. d/zope /Etc/rc2.d/S20zope-> ../init. d/zope /Etc/rc3.d/S20zope-> ../init. d/zope /Etc/rc4.d/S20zope-> ../init. d/zope /Etc/rc5.d/S20zope-> ../init. d/zope |
For other advanced usage, Please man update-rc.d