Overview : CentOS 5 and 6 under the service start, the use of the command is the service name start mode start, in fact, the service command is also called the script under/etc/init.d/, see the specific steps below
Experimental steps:
first prepare the service script, the following piece of code is I wrote earlier, it is similar to the parameters we accept when the service starts, and then returns the output content according to the parameters you entered .
[[Email protected] ~]# vim testsrv #!/bin/bash#function: script test#author : xiaoshui#[ -z $1 ] && echo "Empty" &&exitdir= ' Basename $0 ' dir= "/var/lock/subsys/$dir" var= ' echo $1 | tr ' A-Z ' ' A-Z ' case $ Var instart) if [ ! -e "$Dir" ]; Then touch $Dir && echo "$dir start success" else echo "$dir already start" fi ;; Stop) if [ ! -e "$Dir" ];then echo "$dir is not start " else rm -f $Dir && echo "$dir stop success" fi ;; Restart) if [ ! -e "$Dir" ];then echo "$dir not start " touch $Dir && echo "$dir restart success" else rm $Dir && echo "$dir stopped" touch $Dir && echo "$dir restart success" fi ;; Status) if [ ! -e "$Dir" ];then echo "$dir is stopped " else echo "$dir is running" fi ;; *) echo "argues error,please input start|Restart|stop|status " ;; Esac
The first step: Copy the script to the/etc/init.d/directory and add a line under the previous #!/bin/bash #chkconfig:35 12 88
[Email protected] ~]# vim/etc/init.d/testsrv #!/bin/bash#function:script test#chkconfig:35 88#description:test ser Vice#author:xiaoshui
In addition, the meaning of this content is that 35 indicates the initial level under which to start,-the default is not to start 12 88 means that it represents/etc/rc.d/rc#.d/below the large head of K and S file, which means the priority of the start.
Step Two: Chkconfig--add service adds the service to the Chkconfig list
[Email protected] rc.d]# chkconfig--add testsrv[[email protected] rc.d]# chkconfig--list ... To omit ... testsrv 0:off 1:off 2:off 3:on 4:off 5:on 6:off ..... Omitted.....
Step three: Test with the service command
[[Email protected] rc.d]# service testsrv starttestsrv start Success[[email protected] rc.d]# service Testsrv stoptestsrv Stop Success[[email protected] rc.d]# service testsrv restarttestsrv not starttestsrv restart Success[[email protected] RC . d]# Service Testsrv Statustestsrv is running[[email protected] rc.d]# service testsrv statfsdfsfargues error,please input Start|restart|stop|status
This article is from the "Endless Learning" blog, please be sure to keep this source http://dashui.blog.51cto.com/11254923/1851747
Write a service script under CentOS 5,6