Automatically start scripts when INIT.D service is powered up in CentOS

Source: Internet
Author: User
Let's look at a section of the CentOS in the INIT.D service boot automatically start the script, in fact, also introduced before, I hope to help you.

method One,

Edit/etc/rc.d/rc.local File

Format is program name program path

such as a.sh/home/a.sh

The second method

Just replace the your_prog_name with the name of your actual script or execution file, and set the Prog_path to a specific path.

The code is as follows Copy Code

# Vim/etc/init.d/your_prog_name

#!/bin/bash
#
# Comments to support Chkconfig
# Chkconfig:-98 02
# description:your_prog_name Service Script
#
# Source function library.
. /etc/init.d/functions

### Default variables
Prog_name= "Your_prog_name"
Prog_path= "/usr/bin/${prog_name}"
Pidfile= "/var/run/${prog_name}.pid"
Options= "-C Your_conf_file"

# Check If requirements are met
[-X "${prog_path}"] | | Exit 1

Retval=0

Start () {
Echo-n $ "Starting $prog _name:"
Daemon $prog _path $options
Retval=$?
pid=$ (Pidof ${prog_path})
[!-Z ' ${pid} '] && echo ${pid} > ${pidfile}
Echo
[$RETVAL-eq 0] && touch/var/lock/subsys/$prog _name
Return $RETVAL
}

Stop () {
Echo-n $ "Shutting down $prog _name:"
Killproc-p ${pidfile}
Retval=$?
Echo
[$RETVAL-eq 0] && rm-f/var/lock/subsys/$prog _name
Return $RETVAL
}

Restart () {
Stop
Start
}

Case "$" in
Start
Start
;;
Stop
Stop
;;
Restart)
Restart
;;
Status
Status $prog _path
Retval=$?
;;
*)
echo $ "Usage: $ {Start|stop|restart|status}"
Retval=1
Esac

Exit $RETVAL
# chmod +x/etc/init.d/your_prog_name
# Chkconfig Your_prog_name on

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.