Linux 一個sysv 腳本參考模板

說明:mysql

   1.不少時候咱們的服務都是經過源碼包編譯安裝,可是有的源碼包編譯完成後並不提供該服務的sysv風格腳本,咱們只能手動執其二進制程序+配置文件linux

     2.若是服務器宕機或重啓,就不能自動完成啓動,因此咱們須要本身來編寫腳本並把它放到/etc/init.d/目錄,並使用chkconfig --add $service 加入開機啓動列表web

     3.如下腳本是httpd的一個sysv腳本,不過全部的基本sysv風格腳本都一個風格,你只須要把對httpd的判斷改爲對你編譯的程序判斷便可sql

     4.此腳本也能夠source /etc/init.d/functions 腳本,functions爲咱們提供了三個經常使用的功能,不過如下的這個腳本沒有sourceapache

    a.輸出打印顯示爲統一格式bash

           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]spa

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

     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 "\033[32mNotice: $prog is running now\033[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 "\033[32mOK: $prog is started\033[0m"
 28                 return 0
 29             else
 30                 echo -e "\033[31mError: Failed to start $prog\033[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 "\033[32mOK: $prog is stopped\033[0m"
 44                 return 0
 45             else
 46                 echo -e "\033[31mError: Failed to stop $prog\033[0m"
 47                 return 2
 48             fi
 49     else
 50         echo -e "\033[31mWarning: $prog is not running\033[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 "\033[32mOK: $prog is running ($httpd_pid)\033[0m"
 67         done
 68         return 0
 69     else
 70         echo -e "\033[31mWarning: $prog is not running\033[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 "\033[32mOK: reload $prog is successful\033[0m"
 90                 return 0
 91             else
 92                 echo -e "\033[31mError: Failed to reload $porg\033[0m"
 93                 return 2
 94             fi
 95         else
 96             echo -e "\033[31mError: not reloading $prog due to configuration syntax error\033[0m"
 97             return 2
 98         fi
 99     else
100         start &> /dev/null
101         if [ $? -eq 0 ];then
102             echo -e "\033[32mOK: reload $prog is successful\033[0m"
103             return 0
104         else 
105             echo -e "\033[31mError: Failed to reload $porg\033[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 "\033[31mUsage: `basename $0` {start|stop|restart|reload|status|configtest}\033[0m"                
150     exit 2
151 esac
152         
153         
154             
相關文章
相關標籤/搜索