zoukankan      html  css  js  c++  java
  • Linux 一个sysv 脚本参考模板

    说明:

       1.很多时候我们的服务都是通过源码包编译安装,但是有的源码包编译完成后并不提供该服务的sysv风格脚本,我们只能手动执其二进制程序+配置文件

         2.如果服务器宕机或重启,就不能自动完成启动,所以我们需要自己来编写脚本并把它放到/etc/init.d/目录,并使用chkconfig --add $service 加入开机启动列表

         3.以下脚本是httpd的一个sysv脚本,不过所有的基本sysv风格脚本都一个风格,你只需要把对httpd的判断改成对你编译的程序判断即可

         4.此脚本也可以source /etc/init.d/functions 脚本,functions为我们提供了三个常用的功能,不过以下的这个脚本没有source

        a.输出打印显示为统一格式

               b.daemon 函数  Usage: daemon [+/-nicelevel] {program} 可以向其传递选项 常用的 --user, --pidfile

                   例: daemon --user=mysql  /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf

        c.killproc 函数 Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]

                   例:killproc -p /var/run/mysql /usr/local/mysql/bin/mysqld -HUP  # reload 功能

         5.开启服务程序的reload功能需要,此服务的二进制程序支持接SIGHUP,否则向此服务发出sighup,只能关闭进程,而不能重读配置文件,httpd能够接收hup信号

      1 #!/bin/bash
      2 #
      3 # HTTPD 2.4: Start up the Atlas server daemon
      4 # chkconfig 2345 87 13
      5 # discription: Apache web server for linux
      6 # author: 
      7 #    QQ: 765482322
      8 #    mail: login_532_gajun@sina.com
      9 
     10 install_dir=/usr/local/apache24/instance/httpd80
     11 apachectl=$install_dir/bin/apachectl
     12 binfile=$install_dir/bin/httpd
     13 pidfile=$install_dir/run/httpd.pid
     14 confile=$install_dir/etc/httpd24/httpd.conf
     15 lockfile=$install_dir/lock/httpd
     16 prog=${install_dir##*/}
     17 
     18 function start()
     19 {
     20     if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep" && [ -f $lockfile ];then
     21         echo -e "33[32mNotice: $prog is running now33[0m"
     22         return 0
     23     else
     24         $binfile -f $confile
     25             if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep";then
     26                 touch $lockfile
     27                  echo -e "33[32mOK: $prog is started33[0m"
     28                 return 0
     29             else
     30                 echo -e "33[31mError: Failed to start $prog33[0m"
     31                 return 2
     32             fi
     33     fi
     34 }
     35 
     36 function stop()
     37 {
     38       
     39     if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep" && [ -f $lockfile ];then
     40          ps -ef | grep "$binfile -f $confile" | grep -v "grep" | awk '{print $2}' | xargs kill -9 > /dev/null 2>&1
     41             if [ $? -eq 0 ];then
     42                 rm -f $lockfile
     43                 echo -e "33[32mOK: $prog is stopped33[0m"
     44                 return 0
     45             else
     46                 echo -e "33[31mError: Failed to stop $prog33[0m"
     47                 return 2
     48             fi
     49     else
     50         echo -e "33[31mWarning: $prog is not running33[0m"
     51         return 0
     52     fi
     53 }
     54 
     55 function restart()
     56 {
     57     stop
     58     sleep 1
     59     start
     60 }
     61 
     62 function status()
     63 {
     64     if ps -ef | grep "$binfile -f $confile" | grep -q -v "grep" && [ -f $lockfile ];then
     65         ps -ef | grep "$binfile -f $confile" | grep -v "grep" | awk '{print $2}' | while read httpd_pid;do
     66             echo -e "33[32mOK: $prog is running ($httpd_pid)33[0m"
     67         done
     68         return 0
     69     else
     70         echo -e "33[31mWarning: $prog is not running33[0m"
     71         return 2
     72     fi
     73 }
     74 
     75 function configtest()
     76 {
     77     $apachectl -t -f $confile
     78 }
     79 
     80 function reload()
     81 {
     82     status &> /dev/null
     83     if [ $? -eq 0 ];then
     84         if $apachectl -t -f $confile &> /dev/null;then
     85             ps -ef | grep "$binfile -f $confile" | grep -v "grep" | awk '$1 == "root"{print $2}' | while read httpd_root_pid;do
     86                 kill -HUP $httpd_root_pid
     87             done
     88             if [ $? -eq 0 ] && ps -ef | grep "$binfile -f $confile" | grep -q -v "grep";then
     89                 echo -e "33[32mOK: reload $prog is successful33[0m"
     90                 return 0
     91             else
     92                 echo -e "33[31mError: Failed to reload $porg33[0m"
     93                 return 2
     94             fi
     95         else
     96             echo -e "33[31mError: not reloading $prog due to configuration syntax error33[0m"
     97             return 2
     98         fi
     99     else
    100         start &> /dev/null
    101         if [ $? -eq 0 ];then
    102             echo -e "33[32mOK: reload $prog is successful33[0m"
    103             return 0
    104         else 
    105             echo -e "33[31mError: Failed to reload $porg33[0m"
    106             return 2
    107         fi
    108     fi
    109 }        
    110 
    111 case $1 in
    112 start)
    113     start
    114     if [ $?    -eq 2 ];then
    115         exit 2
    116     fi
    117     ;;
    118 
    119 stop)
    120     stop
    121     if [ $?    -eq 2 ];then
    122         exit 2
    123     fi
    124     ;;
    125 
    126 restart)
    127     restart
    128     if [ $? -eq 2 ];then
    129         exit 2
    130     fi
    131     ;;
    132 
    133 reload)
    134     reload
    135     if [ $? -eq 2 ];then
    136         exit 2
    137     fi
    138     ;;
    139 
    140 status)
    141     status
    142     ;;
    143 
    144 configtest)
    145     configtest
    146     ;;
    147 
    148 *)
    149     echo -e "33[31mUsage: `basename $0` {start|stop|restart|reload|status|configtest}33[0m"                
    150     exit 2
    151 esac
    152         
    153         
    154             
  • 相关阅读:
    @Target:注解的作用目标
    Node.js学习笔记(2)
    Node.js学习笔记(1)
    javascript小记-javascript运行机制
    javascript小记-作用域
    javascript小记-闭包理解
    php中ajax跨域请求---小记
    饼状图一
    QPainter使用不同风格的QBrush来填充区域
    QPainter绘制特殊线条
  • 原文地址:https://www.cnblogs.com/M18-BlankBox/p/5881815.html
Copyright © 2011-2022 走看看