Write INIT.D service script on CentOS

Source: Internet
Author: User


Background:
Wrote some scripts, downloaded some open-source software, want to make them into a system service, through the service Your_prog_name start way to run the background, and automatically start when the boot.
After I learned about the daemon command, I found it handy to write such a script, as shown in the following example:

Specific configuration:
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.
# Vim/etc/init.d/your_prog_name

#!/bin/bash # Comments to support Chkconfig # Chkconfig:-# 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 ${pid
  File} retval=$? echo [$RETVAL-eq 0] && rm-f/var/lock/subsys/$prog _name return $RETVAL} restart () {Stop start} CA
  Se "in start" start;;
  stop) stop;;
  restart) restart;;
 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


Turn from: http://ju.outofmemory.cn/entry/95057


Additional service TOMCAT1 Start example:


#!/bin/sh
#
# chkconfig:2345 85 21
# description:zimg Startup scripts
#
tomcat= "/USR/SERVER/TOMCAT1"
startup= "/usr/server/tomcat1/bin/startup.sh"
Shutdown= "/usr/server/tomcat1/bin/shutdown.sh"
Export java_home=/usr/local/jdk1.7.0
if [-f/etc/rc.d/init.d/functions]; Then
. /etc/rc.d/init.d/functions
Fi

Start () {
CD $tomcat
$startup
echo "TOMCAT1 started."
}

Stop () {

CD $tomcat
$shutdown
echo "Tomcat1 stopped."

}
# How we were called.
Case "$" in
Start
Start
;;
Stop
Stop
;;
Status
;;
Restart)
Stop
Start
;;
Condrestart)
;;
Reload
;;
*)
echo $ "Usage: $ {start|stop|status|restart|condrestart|reload}"
;;
Esac
Exit $RETVAL
~
~
~
~

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.