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
~
~
~
~