[root@ns dirbash]# cat /etc/init.d/http2.sh #!/bin/bash #chkconfig: - 99 88 #description: this the httpd script of myself. #前面两行是脚本加入chkconfig 管理必须的注释行,否则报错 lockfile=/var/lock/subsys/httpd #锁文件,用来判断http服务是否启动 pidfile=/var/run/httpd/httpd.pid #http服务的pid httpd=/usr/sbin/httpd #http服务的启动脚本 prog=httpd timedelay=`sleep 1` #延迟1秒 . /etc/init.d/functions #调用系统的公共函数function; 目的:使用killproc 命令 start (){ #启动http服务函数 if [ -e $lockfile ] ;then echo "the program $prog is running..." else echo -n "Starting $prog :" $timedelay $httpd && touch $lockfile && echo -e "\t\t\t[\033[32m ok \033[0m"] || echo -e "\t\t\t[\033[31m fail \033[0m]" fi } stop () { #关闭http服务函数 if [ -e $lockfile ];then echo -n "Stopping $prog :" $timedelay killproc $prog &>/dev/null && rm -rf $lockfile && echo -e " \t\t\t[\033[32m ok \033[0m"] || echo -e "\t\t\t[\033[31m fail \033[0m]" else echo -e "Stopping $prog :\t\t\t[\033[31m fail\033[0m]" fi } status () { #http服务状态函数 if [ -e $pidfile ];then echo "the program $prog (`cat $pidfile`) is running." else echo "the program $prog is stop." fi } case "$1" in start) start ;; stop) stop ;; restart) stop start ;; status) status ;; *) echo "Usage: $prog [start|stop|restart|status]" ;; esac [root@ns dirbash]# chkconfig --add http2.sh^C [root@ns dirbash]# service http2.sh start the program httpd is running... [root@ns dirbash]# netstat -tulpn |grep 80 tcp 0 0 :::80 :::* LISTEN 10137/httpd [root@ns dirbash]# servcie http2.sh stop -bash: servcie: command not found [root@ns dirbash]# service http2.sh stop Stopping httpd : [ ok ] [root@ns dirbash]# service http2.sh restart Stopping httpd : [ fail] Starting httpd : [ ok ] [root@ns dirbash]# service http2.sh restart Stopping httpd : [ ok ] Starting httpd : [ ok ] [root@ns dirbash]# service http2.sh status the program httpd (10224) is running. [root@ns dirbash]# service http2.sh stop Stopping httpd : [ ok ] [root@ns dirbash]# service http2.sh status the program httpd is stop. [root@ns dirbash]# service http2.sh aaa Usage: httpd [start|stop|restart|status]
转载于:https://blog.51cto.com/feilong0663/1350979