tengine的系統服務啓動腳本

1.tengine的系統服務啓動腳本:

#!/bin/sh
#
# tengine - this script starts and stops the tengine daemon
#
# chkconfig:   - 86 19
# description:  Tengine is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# scriptname: /etc/init.d/tengine
#
#################################################################
#
# processname: tengine
# attention: this tengine's prefix path installed is "/application/tengine".(/application/tengine is a soft link of /application/nginx)
# config:      /application/tengine/conf/tengine.conf
# config:      /etc/sysconfig/tengine
# pidfile:     /application/tengine/logs/tengine.pid
#################################################################
#
#################################################################
# The method of installing tengine is done below:
#阿里雲鏡像:
#https://mirrors.aliyun.com 即:https://opsx.alibaba.com/mirror
#	CentOS yum源配置方法:
#	1、備份:
#		mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#	2、下載新的CentOS-Base.repo 到/etc/yum.repos.d/:
#	  CentOS 6:
#		wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
#		或者
#		curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
#	  CentOS 7:
#		wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#		或者
#		curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#	3、之後運行yum makecache生成緩存
#	4、其他:
#		非阿里雲ECS用戶會出現 Couldn't resolve host 'mirrors.cloud.aliyuncs.com' 信息,不影響使用。用戶也可自行修改相關配置: eg:
#		sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
#
#	CentOS epel
#	epel 配置方法
#	1、備份(如有配置其他epel源):
#		mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
#		mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
#	2、下載新repo 到/etc/yum.repos.d/:
#		epel(RHEL 7)
#			wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#		epel(RHEL 6)
#			wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
#		epel(RHEL 5)
#			wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-5.repo
#
#su - root
#yum -y install lrzsz dos2unix nmap nc tree telnet
#mkdir -p /root/tools && cd /root/tools
#wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz
#mkdir -p /root/source
#tar xzvf tengine-2.1.2.tar.gz -C /root/source/
#cd /root/source/tengine-2.1.2
#mkdir -p /application/nginx
#useradd -s /sbin/nologin -r  -M tengine
#yum -y install gcc openssl-devel pcre-devel zlib-devel libxml2-devel libxslt-devel gd-devel lua-devel geoip-devel	
#./configure    --help
#./configure    --prefix=/application/nginx --user=tengine --group=tengine --enable-mods-static=all --enable-mods-shared=all
#make && make install && echo -e "\n\nInstallation is OK"
#
#
#################################################################
#
#importantly,this script is writed and modified according to the starting script of nginx's service(such as /etc/init.d/nginx).
#
#author:freeflybird
#blog:https://blog.csdn.net/CPPCPPCPPCPP
#################################################################

# required to use "root" user.
[ $UID -ne 0 ] && {
		echo "You must use the \"root\" user to operate."
		exit 1
}

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

# define working directory.
nginxPrefix="/application/nginx"
tenginePrefix="/application/tengine"

# create soft links.
if [ ! -d $nginxPrefix ];then
	exit 1
else
	[ -e $tenginePrefix ] || ln -s $nginxPrefix/ $tenginePrefix 
	[ -e $tenginePrefix/sbin/tengine ] || ln -s $tenginePrefix/sbin/nginx $tenginePrefix/sbin/tengine
	[ -e $tenginePrefix/conf/tengine.conf ] || ln -s $tenginePrefix/conf/nginx.conf $tenginePrefix/conf/tengine.conf
fi

tengine="$tenginePrefix/sbin/tengine"
prog=$(basename $tengine)

sysconfig="/etc/sysconfig/$prog"
lockfile="$tenginePrefix/logs/tengine"
pidfile="$tenginePrefix/logs/${prog}.pid"

TENGINE_CONF_FILE="$tenginePrefix/conf/tengine.conf"

#################################
# create system config file of tengine.
if [ ! -f $sysconfig ];then 

cat >$sysconfig<<EOF
# Configuration file for the tengine service

# set this to the location of the tengine configuration file
TENGINE_CONF_FILE=$tenginePrefix/conf/tengine.conf
EOF

fi
#################################

grep "${prog}.pid" $tenginePrefix/conf/nginx.conf &>>/dev/null
if [ $? -ne 0 ];then
	sed -i '9a pid        logs\/tengine.pid;'  $tenginePrefix/conf/nginx.conf
fi


[ -f $sysconfig ] && . $sysconfig


start() {
	[ -x $tengine ] || exit 5
	[ -f $TENGINE_CONF_FILE ] || exit 6
	echo -n $"Starting $prog: "
	daemon $tengine -c $TENGINE_CONF_FILE
	retval=$?
	echo
	[ $retval -eq 0 ] && touch $lockfile
	return $retval
}

stop() {
	echo -n $"Stopping $prog: "
	killproc -p $pidfile $prog
	retval=$?
	echo
	[ $retval -eq 0 ] && rm -f $lockfile
	return $retval
}

restart() {
	configtest_q || return 6
	stop
	start
}

reload() {
	configtest_q || return 6
	echo -n $"Reloading $prog: "
	killproc -p $pidfile $prog -HUP
	echo
}

configtest() {
	$tengine -t -c $TENGINE_CONF_FILE
}

configtest_q() {
	$tengine -t -q -c $TENGINE_CONF_FILE
}

rh_status() {
	status $prog
}

rh_status_q() {
	rh_status >/dev/null 2>&1
}

# Upgrade the binary with no downtime.
upgrade() {
	local oldbin_pidfile="${pidfile}.oldbin"

	configtest_q || return 6
	echo -n $"Upgrading $prog: "
	killproc -p $pidfile $prog -USR2
	retval=$?
	sleep 1
	if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
		killproc -p $oldbin_pidfile $prog -QUIT
		success $"$prog online upgrade"
		echo 
		return 0
	else
		failure $"$prog online upgrade"
		echo
		return 1
	fi
}

# Tell tengine to reopen logs
reopen_logs() {
	configtest_q || return 6
	echo -n $"Reopening $prog logs: "
	killproc -p $pidfile $prog -USR1
	retval=$?
	echo
	return $retval
}

case "$1" in
	start)
		rh_status_q && exit 0
		$1
		;;
	stop)
		rh_status_q || exit 0
		$1
		;;
	restart|configtest|reopen_logs)
		$1
		;;
	force-reload|upgrade) 
		rh_status_q || exit 7
		upgrade
		;;
	reload)
		rh_status_q || exit 7
		$1
		;;
	status|status_q)
		rh_$1
		;;
	condrestart|try-restart)
		rh_status_q || exit 7
		restart
		;;
	*)
		echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
		exit 2
esac

2.執行效果:

[root@linux ~]# ls -lrth /application/
total 4.0K
drwxr-xr-x 10 root root 4.0K Aug  4 12:34 nginx
lrwxrwxrwx  1 root root   19 Aug  4 17:16 tengine -> /application/nginx/
[root@linux ~]# ls -lrth /application/tengine/
total 32K
drwxr-xr-x 2 root    root 4.0K Aug  4 12:31 html
drwxr-xr-x 2 root    root 4.0K Aug  4 12:31 modules
drwxr-xr-x 2 root    root 4.0K Aug  4 12:31 include
drwx------ 2 tengine root 4.0K Aug  4 12:34 proxy_temp
drwx------ 2 tengine root 4.0K Aug  4 12:34 client_body_temp
drwxr-xr-x 2 root    root 4.0K Aug  4 17:16 sbin
drwxr-xr-x 2 root    root 4.0K Aug  4 17:16 conf
drwxr-xr-x 2 root    root 4.0K Aug  5 08:48 logs
[root@linux ~]# ls -lrth /application/tengine/conf/
total 72K
-rw-r--r-- 1 root root 2.2K Aug  4 12:31 koi-win
-rw-r--r-- 1 root root 2.8K Aug  4 12:31 koi-utf
-rw-r--r-- 1 root root 3.6K Aug  4 12:31 win-utf
-rw-r--r-- 1 root root 4.0K Aug  4 12:31 mime.types
-rw-r--r-- 1 root root 4.0K Aug  4 12:31 mime.types.default
-rw-r--r-- 1 root root  964 Aug  4 12:31 fastcgi_params
-rw-r--r-- 1 root root  964 Aug  4 12:31 fastcgi_params.default
-rw-r--r-- 1 root root 1.1K Aug  4 12:31 fastcgi.conf
-rw-r--r-- 1 root root 1.1K Aug  4 12:31 fastcgi.conf.default
-rw-r--r-- 1 root root  623 Aug  4 12:31 uwsgi_params
-rw-r--r-- 1 root root  623 Aug  4 12:31 uwsgi_params.default
-rw-r--r-- 1 root root  596 Aug  4 12:31 scgi_params
-rw-r--r-- 1 root root  596 Aug  4 12:31 scgi_params.default
-rw-r--r-- 1 root root 2.8K Aug  4 12:31 nginx.conf.default
-rw-r--r-- 1 root root 5.1K Aug  4 12:31 browsers
-rw-r--r-- 1 root root 3.3K Aug  4 12:31 module_stubs
lrwxrwxrwx 1 root root   36 Aug  4 17:16 tengine.conf -> /application/tengine/conf/nginx.conf
-rw-r--r-- 1 root root 2.8K Aug  4 17:16 nginx.conf
[root@linux ~]# ls -lrth /application/tengine/sbin/
total 4.6M
-rwxr-xr-x 1 root root 4.6M Aug  4 12:31 nginx
-rwxr-xr-x 1 root root  19K Aug  4 12:31 dso_tool
lrwxrwxrwx 1 root root   31 Aug  4 17:16 tengine -> /application/tengine/sbin/nginx
[root@linux ~]# ls -lrth /application/tengine/logs/
total 16K
-rw-r--r-- 1 root root  833 Aug  4 12:56 error.log
-rw-r--r-- 1 root root 4.3K Aug  4 19:54 access.log
-rw-r--r-- 1 root root    5 Aug  5 08:48 tengine.pid
-rw-r--r-- 1 root root    0 Aug  5 08:48 tengine
[root@linux ~]# 
[root@linux ~]# /etc/init.d/tengine 
Usage: /etc/init.d/tengine {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}
[root@linux ~]# /etc/init.d/tengine status
tengine is stopped
[root@linux ~]# /etc/init.d/tengine start
Starting tengine: [  OK  ]
[root@linux ~]# /etc/init.d/tengine status
tengine (pid 1672 1670) is running...
[root@linux ~]# ps -ef | grep tengine | grep -v grep
root       1670      1  0 08:46 ?        00:00:00 nginx: master process /application/tengine/sbin/tengine -c /application/tengine/conf/tengine.conf
tengine    1672   1670  0 08:46 ?        00:00:00 nginx: worker process                                                      
[root@linux ~]# /etc/init.d/tengine stop
Stopping tengine: [  OK  ]
[root@linux ~]# /etc/init.d/tengine status
tengine is stopped
[root@linux ~]# ps -ef | grep tengine | grep -v grep
[root@linux ~]# /etc/init.d/tengine restart
Stopping tengine: [FAILED]
Starting tengine: [  OK  ]
[root@linux ~]# /etc/init.d/tengine status
tengine (pid 1734 1732) is running...
[root@linux ~]# ps -ef | grep tengine | grep -v grep
root       1732      1  0 08:47 ?        00:00:00 nginx: master process /application/tengine/sbin/tengine -c /application/tengine/conf/tengine.conf
tengine    1734   1732  0 08:47 ?        00:00:00 nginx: worker process                                                      
[root@linux ~]# /etc/init.d/tengine reload
Reloading tengine: [  OK  ]
[root@linux ~]# /etc/init.d/tengine status
tengine (pid 1763 1732) is running...
[root@linux ~]# ps -ef | grep tengine | grep -v grep
root       1732      1  0 08:47 ?        00:00:00 nginx: master process /application/tengine/sbin/tengine -c /application/tengine/conf/tengine.conf
tengine    1763   1732  0 08:47 ?        00:00:00 nginx: worker process                                                      
[root@linux ~]# /etc/init.d/tengine configtest
the configuration file /application/tengine/conf/tengine.conf syntax is ok
configuration file /application/tengine/conf/tengine.conf test is successful
[root@linux ~]# 
[root@linux ~]# /etc/init.d/tengine restart
Stopping tengine: [  OK  ]
Starting tengine: [  OK  ]
[root@linux ~]# /etc/init.d/tengine status
tengine (pid 1805 1803) is running...
[root@linux ~]# ps -ef | grep tengine | grep -v grep
root       1803      1  0 08:48 ?        00:00:00 nginx: master process /application/tengine/sbin/tengine -c /application/tengine/conf/tengine.conf
tengine    1805   1803  0 08:48 ?        00:00:00 nginx: worker process                   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章