编写CentOS 5,6下的服务脚本

前端之家收集整理的这篇文章主要介绍了编写CentOS 5,6下的服务脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

概述:CentOS 5和6下的服务启动时,使用的命令都是service 服务名 start方式启动,其实service命令也是调用了/etc/init.d/下的脚本,下面请看具体步骤

实验步骤:

首先准备好服务脚本,下面这一段代码是我之前写的,其类似于我们的服务启动时接受的参数,然后根据你所输入的参数来返回输出内容

[root@localhost~]#vimtestsrv
#!/bin/bash
#function:scripttest
#author:xiaoshui
#[-z$1]&&echo"empty"&&exit
dir=`basename$0`
Dir="/var/lock/subsys/$dir"
var=`echo$1|tr'A-Z''a-z'`
case$varin
start)
if[!-e"$Dir"];then
touch$Dir&&echo"$dirstartsuccess"
else
echo"$diralreadystart"
fi
;;
stop)
if[!-e"$Dir"];then
echo"$dirisnotstart"
else
rm-f$Dir&&echo"$dirstopsuccess"
fi
;;
restart)
if[!-e"$Dir"];then
echo"$dirnotstart"
touch$Dir&&echo"$dirrestartsuccess"
else
rm$Dir&&echo"$dirstopped"
touch$Dir&&echo"$dirrestartsuccess"
fi
;;
status)
if[!-e"$Dir"];then
echo"$dirisstopped"
else
echo"$dirisrunning"
fi
;;
*)
echo"argueserror,pleaseinputstart|restart|stop|status"
;;
esac

第一步:将脚本拷贝至/etc/init.d/目录下,并在前面的#!/bin/bash下加上一行#chkconfig:35 12 88

[root@localhost~]#vim/etc/init.d/testsrv
#!/bin/bash
#function:scripttest
#chkconfig:351288
#description:testservice
#author:xiaoshui

加上内容的意义在于,35表示初始在哪个级别下启动,-表示默认都不启动 12 88 表示其表示/etc/rc.d/rc#.d/下面的K和S大头的文件,其含义就是启动的优先级。

第二步:chkconfig --add 服务 将服务加到chkconfig列表中

[root@localhostrc.d]#chkconfig--addtestsrv
[root@localhostrc.d]#chkconfig--list
......省略........
testsrv0:off1:off2:off3:on4:off5:on6:off
.......省略.....

第三步:使用service命令进行测试

[root@localhostrc.d]#servicetestsrvstart
testsrvstartsuccess
[root@localhostrc.d]#servicetestsrvstop
testsrvstopsuccess
[root@localhostrc.d]#servicetestsrvrestart
testsrvnotstart
testsrvrestartsuccess
[root@localhostrc.d]#servicetestsrvstatus
testsrvisrunning
[root@localhostrc.d]#servicetestsrvstatfsdfsf
argueserror,pleaseinputstart|restart|stop|status
原文链接:https://www.f2er.com/centos/380766.html

猜你在找的CentOS相关文章