Linux_自制系统服务启动脚本

目录

前言

在Linux的某些系统服务中,需要自己定制启动服务的脚本。通常会使用Cash语句来实现。

Case语句

一般用于程序启动脚本
Syntax:

case $1 in
    Param1)
        Commands
        ;;
    Param2)
        Commands
        ;;
    *)
        Commands
    esac

Example:

#!/bin/bash -e
#/bin/bash -e 表示系统发生第一个错误时就中止脚本执行
#每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。
# chkconfig:35 12 45
#第一行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用 – 代替运行级。
# description:Service start script
#第二行对服务进行描述,可以用 跨行注释。
RETVAL=0
case $1 in
start)
  echo "service starting..."
  ;;
stop)
  echo "service stopping..."
  ;;
restart)
#$0 meating is this one script
  sh $0 stop || true
# $0 stop || ture  表示出现错误时候不想中止的指令
  sh $0 start
  ;;
*)
  echo "input syntax error!"
  echo "Usage:Is [start|stop|restart]"
  exit 1
  ;;
esac
echo $RETVAL
###################################SCRIPT END

Apache 启动脚本

######################################## Apache 启动脚本
#!/bin/bash -e       
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions  
RETVAL=0  #使用变量作为判断和关联上下本的载体
httpd="/application/apache/bin/httpd"    #使用变量简化使用指令的决定路径
start() {  
        $httpd -k start >/dev/null 2>&1    #httpd -k start|restart|graceful|stop|graceful-stop     发送信号使httpd启动、重新启动或停止
        # daemon httpd >/dev/null 2>&1   # 2>&1 将错误输出到正确输出,即标准输出和错误输出一起输出,管道|不通过错误输出
        RETVAL=$?  
        [ $RETVAL -eq 0 ] && action "启动 httpd:" /bin/true ||  
        action "启动 httpd:" /bin/false  
        return $RETVAL  
}  

stop() {  
        $httpd -k stop >/dev/null 2>&1  
        # killproc httpd >/dev/null 2>&1  
        [ $? -eq 0 ] && action "停止 httpd:" /bin/true ||  
        action "停止 httpd:" /bin/false  
        return $RETVAL  
}  
case "$1" in  
  start)  
        start  #Call function start()
        ;;  
  stop)  
        stop  
        ;;  
  restart)  
       sh $0 stop  
       sh $0 start  
        ;;  
   *)  
        echo "Format error!"  
        echo $"Usage: $0 {start|stop|restart}"  
        exit 1  
        ;;  
esac  
exit $RETVAL  
####################################### SCRIPT END

Postfix service 启停脚本

################################ Postfix service 启停脚本
#!/bin/bash -e
# chkconfig:35 53 55
# discription:postfix
start() {
        echo "Starting postfix..."
        postfix start &> /dev/null
        echo "OK!"
}
stop() {
        echo -n "stopping postfix..."
        postfix stop &> /dev/null
        echo "OK!"
}
reload() {
        echo -n "Loading postfix configure file"
        postfix reload &> /dev/null
        echo "OK!"
}
status() {
        postfix status &> /dev/null
        if [ $? -eq 0 ]
        then echo "running!"
        else echo "stop!"
        if
}
help() {
        echo "syntax error!"
        echo "Uasge:Is [start|stop|restart|reload|status]"
}
case $1 in
start)
        $1
        ;;
stop)
        $1
        ;;
restart)
        stop
        start
        ;;
reload)
        $1
        ;;
status)
        $1
        ;;
*)
        help
        ;;
esac
################################SCRIPT END
原文地址:https://www.cnblogs.com/jmilkfan-fanguiju/p/7533745.html