[笔记] /etc/init.d/ 下脚本的通用结构
http://sunxiaqw.blog.163.com/blog/static/99065438201111715813443/
下面以 named 为例 :
#!/bin/bash
#
# named This shell script takes care of starting and stopping # 注释 :给出脚本的作用
# named (BIND DNS server).
#
# chkconfig: - 55 45 # 注释 :给出 named 的启动/关闭级别 ,例如
/etc/rc3.d/S55named -> ../init.d/named
/etc/rc6.d/K45named -> ../init.d/named
chkconfig: 345 85 15 (这个比较有意思,345代表在设置在那个level中是on的,如果一个都不想on,那就写一个横线"-",比如:chkconfig: - 85 15。后面两个数字当然代表S和K的默认排序号啦)
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# pidfile: /var/run/httpd.pid
# config: /usr/local/apache2/conf/httpd.conf (我装东西比较喜欢都放在/usr/local下)
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
INITLOG_ARGS=""
apachectl=/usr/local/apache2/bin/apachectl
httpd=${HTTPD-/usr/local/apache2/bin/httpd}
prog="httpd"
start()
{
ps -ef | grep "$prog" | grep -v "grep" | grep -v "start" > /dev/Null
if [ "$?" -eq "1" ]; then
echo -n $"Starting $prog: "
daemon $httpd $OPTIONS
ret=$?
echo
[ "$ret" -eq "0" ] && touch /var/lock/subsys/httpd
return 0
else
echo "httpd running"
return 0
fi
}
stop()
{
status httpd >/dev/Null >&1
ret_status=$?
if [ $ret_status -eq 2 ]; then
rm -f /var/lock/subsys/httpd /var/run/httpd.pid
echo "httpd stopped"
return 0
elif [ $ret_status -eq 3 ]; then
echo "httpd stopped"
return 0
fi
echo -n $"Stopping $prog: "
killproc $httpd
ret=$?
echo
[ "$ret" -eq "0" ] && rm -f /var/lock/subsys/httpd /var/run/httpd.pid
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status httpd
;;
http://msnpiki.msnfanatic.com/index.php/Main_Page-->
详细出处参考:http://www.itqun.net/content-detail/194818.html
# description: named (BIND) is a Domain Name Server (DNS) \ # 注释 ;描述所启动的服务的作用
# that is used to resolve host names to IP addresses.
# probe: true
# Source function library. # 注释 :这个也是固定的,functions 脚本提供了公用的函数
. /etc/rc.d/init.d/functions
# Source networking configuration. # 注释 :这个也比较常用,用于获得网络的一些基本信息
[ -r /etc/sysconfig/network ] && . /etc/sysconfig/network
RETVAL=0 # 注释 :
prog="named" # 注释 :
# Check that networking is up. # 注释 :检查网络是否已经启动
[ "${NETWORKING}" = "no" ] && exit 1 # 注释 :NETWORKING 变量来自于 /etc/sysconfig/network文件
如果等于 no 则退出,返回 1
[ -r /etc/sysconfig/named ] && . /etc/sysconfig/named # 注释 :如果存在 /etc/sysconfig/named 且可读,则执行它
该文件保存了 named 启动的选项
[ -x /usr/sbin/named ] || exit 1 # 注释 :如果存在 /usr/sbin/named 且继续,否则退出,返回1
[ -r ${ROOTDIR}/etc/named.conf ] || exit 1 # 注释 :如果存在 $ROOTDIR/etc/named.conf 且可读,则继续,否则
返回1。ROOTDIR 变量来自于 /etc/sysconfig/named文件
下面是最重要的函数定义,有 start()、stop()、status()、reload()、restart()probe()
下面是 case 语句 ,就是 start、stop、reload、restart 这些了