| 事先準備工作:源碼安裝apache 。安裝目錄為/usr/local/httpd 任務(wù)需求:1、可通過 service httpd start|stop|status|restart 命令對服務(wù)進行控制
 2、httpd服務(wù)可開機自啟動 思路:1、start、stop操作可直接調(diào)用源碼安裝的httpd的控制程序apachectl
 2、在啟動服務(wù)時,建立httpd.lock文件;停止服務(wù)時刪除
 3、status操作檢測httpd.lock文件是否存在,存在判斷服務(wù)已啟動,不存在表示服務(wù)停止
 4、對每個操作建立對應(yīng)的函數(shù),進行調(diào)用
 5、restart操作先調(diào)用stop函數(shù),在調(diào)用start函數(shù)
 6、服務(wù)腳本的控制參數(shù)通過位置變量 $1 傳入,使用case分支進行識別、執(zhí)行相應(yīng)的操作
 7、在腳本開頭添加chkconfig管理參數(shù),定義哪個運行級別啟動、服務(wù)啟動優(yōu)先級、服務(wù)關(guān)閉優(yōu)先級(讓服務(wù)開機自啟動,必須添加),description服務(wù)描述,進程名
 PS:看過系統(tǒng)已有系統(tǒng)服務(wù)腳本,發(fā)現(xiàn)那些比我寫的復雜多了。原諒我剛學shell,第一次寫系統(tǒng)服務(wù)腳本 腳本如下: #vim /etc/init.d/httpd#!bin/bash
 #chkconfig:2345 55 25     //運行級別、啟動優(yōu)先級、關(guān)閉優(yōu)先級
 #processname:httpd        //進程名
 #description:source httpd server daemon  //服務(wù)描述
 prog=/usr/local/httpd/bin/apachectl      //控制程序路徑
 lock=/usr/local/httpd/httpd.lock         //lock文件路徑
 start(){                                 //start函數(shù)
 $prog start
 echo "正在啟動服務(wù)...."
 touch $lock
 }
 stop(){                                //stop函數(shù)
 $prog stop
 echo "正在停止服務(wù)...."
 rm -rf $lock
 }
 status(){                        //status函數(shù)
 if [ -e $lock ];then
 echo "$0 服務(wù)正在運行"
 else
 echo "$0 服務(wù)已經(jīng)停止"
 fi
 }
 restart(){              //restart函數(shù)
 stop
 start          //直接調(diào)用stop、start函數(shù),
 }
 case "$1" in         //case分支結(jié)構(gòu)匹配,$1位置參數(shù)對控制參數(shù)調(diào)用
 "start")
 start       //調(diào)用start函數(shù)
 ;;
 "stop")             //調(diào)用stop函數(shù)
 stop
 ;;
 "status")             //調(diào)用status函數(shù)
 status
 ;;
 "restart")            //調(diào)用restart函數(shù)
 restart
 ;;
 *)                 //其他參數(shù)就輸出腳本正確用法
 echo "用法:$0 start|stop|status|restart"
 ;;
 esac
 驗證:[root@ndbA /]# service httpd start
 正在啟動服務(wù)....
 [root@ndbA /]# service httpd status
 /etc/init.d/httpd 服務(wù)正在運行
 [root@ndbA /]# service httpd stop
 正在停止服務(wù)....
 [root@ndbA /]# service httpd status
 /etc/init.d/httpd 服務(wù)已經(jīng)停止
 [root@ndbA /]# service httpd stop
 httpd (no pid file) not running
 正在停止服務(wù)....
 [root@ndbA /]# service httpd restatus
 用法:/etc/init.d/httpd start|stop|status|restart
 [root@ndbA /]# service httpd restart
 httpd (no pid file) not running
 正在停止服務(wù)....
 正在啟動服務(wù)....
 [root@ndbA /]#
 [root@ndbA /]# chkconfig --list httpdhttpd           0:關(guān)閉  1:關(guān)閉  2:啟用  3:啟用  4:啟用  5:啟用  6:關(guān)閉
 [root@ndbA /]# chkconfig  httpd off
 [root@ndbA /]# chkconfig --list httpd
 httpd           0:關(guān)閉  1:關(guān)閉  2:關(guān)閉  3:關(guān)閉  4:關(guān)閉  5:關(guān)閉  6:關(guān)閉
 [root@ndbA /]# chkconfig  httpd on
 [root@ndbA /]# chkconfig --list httpd
 httpd           0:關(guān)閉  1:關(guān)閉  2:啟用  3:啟用  4:啟用  5:啟用  6:關(guān)閉
 [root@ndbA /]#
   |