How to set the Centos program to automatically start

Source: Internet
Author: User
Tags centos

In CentOS, there are two main ways to set the startup of a self-installed program.

1. Add the startup program command to the/etc/rc. d/rc. local file. For example, set the startup httpd and record the startup time.

The code is as follows: Copy code

#! /Bin/sh

#

# This script will be executed * after * all the other init scripts.

# You can put your own initialization stuff in here if you don't

# Want to do the full Sys V style init stuff.

Touch/var/lock/subsys/local

/Usr/local/apache/bin/apachectl start

Date>/root/rtime.txt

2. Add the prepared startup script to the/etc/rc. d/init. d/directory, and run the command chkconfig to set the startup.

For example, we write the httpd script and put it in the/etc/rc. d/init. d/directory.

The code is as follows: Copy code


Cd/etc/rc. d/init. d
Vi youshell. sh # change youshell. sh to your script name

Write your own scripts and save and exit.
Before writing a script, add the following annotations:

The code is as follows: Copy code

# Add for chkconfig
# Chkconfig: 2345 70 30
# Description: the description of the shell # brief description of the script
# Processname: servicename # Name of the first process, which will be used when self-starting is set later

Note:
2345 indicates the script running level, that is, it can be run in the four modes of 2345, 234 is a text interface, and 5 is a graphical interface X
70 indicates the sequence number of the script to be started in the future. If the sequence number of other programs is smaller than 70 (such as 44 and 45), the script must be started after these programs are started.
30 indicates the stop sequence number of the script when the system is disabled.

Add executable permissions to the script:

The code is as follows: Copy code

Chmod + x youshell. sh

Use the chkconfig command to set the script to self-start

The code is as follows: Copy code

Chkconfig -- add servicename

In this way, your script runs automatically after it is started.

In addition, you can use this method in redhat to enable auto-start upon startup.

The actual command is very simple, just use chkconfig. For example, you need to set mysqld to automatically start upon startup:
Bytes ----------------------------------------------------------------------------------------------

The code is as follows: Copy code
# Chkconfig mysqld on

Similarly, to cancel automatic startup of a service, you only need to change the "on" parameter to "Off. For example, to cancel automatic start of postfix:

# Chkconfig postfix off

It is worth noting that if this service has not been added to the chkconfig list, you need to add it using the-add parameter:

The code is as follows: Copy code

# Chkconfig-add postfix

To query all the automatically started Services, enter:

The code is as follows: Copy code

# Chkconfig-list

However, this shows that there are too many things and it looks dizzy. What if I only want to view the specified service? In this case, you only need to add the service name after "-list". For example, to check whether the httpd service is automatically started, enter:

The code is as follows: Copy code

# Chkconfig-list httpd

Output result at this time:

The code is as follows: Copy code

Httpd 0: off 1: off 2: off 3: off 4: off 5: off 6: off

At this time, 0 ~ If both are off, the httpd service will not be automatically started when the system starts. After entering chkconfig httpd on, we will check again that the output result is:

The code is as follows: Copy code

Httpd 0: off 1: off 2: on 3: on 4: on 5: on 6: off

2 ~ If all five are on, it indicates that it will be automatically started.

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.