8月 02

linux远程通过ssh启停tomcat

  在linux上远程通过ssh启停tomcat,因为存在环境变量加载问题,所以写一个shell脚本,放在tomcat的bin目录下。CentOS测试通过。需要使用bash。因为脚本中包含脚本名字,在查找时要过滤掉自己,所以如果需要修改脚本名字,在脚本内也要相应的修改。

#!/bin/bash
# -------------------------------------------------------------------------------
# Filename:    tomcatcomm.sh
# Revision:    1.0
# Date:        2013-7-29
# Author:      simonzhang
# Email:       simon-zzm@163.com
# Description: 
# -------------------------------------------------------------------------------
# Source function library.
. /etc/init.d/functions
source /etc/profile

#### shell env
path=$(dirname $0)
if [ ${path:0:1} == . ]; then
   path=${path/./$(pwd)}
fi
my_name="tomcatcomm.sh"

###### delete tomcat`s work dir and startup server
del_start()
{
    /bin/rm -rf ${1}work/*
}

######main()
case "$1" in
  start)
       get_pid_count=`/bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|wc -l`
       if [ ${get_pid_count} -gt 0 ] ; then
          /bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|awk ' ''{print $2}'|xargs kill -9
          sleep 1
          get_pid_count=`/bin/ps -ef|grep -v grep|grep -v ${my_name}|grep ${path}|wc -l`
           if [ ${get_pid_count} -gt 0 ] ; then
              echo "Error"
           else
              del_start ${path}
              ${path}/startup.sh
           fi
        else
            del_start ${path}
            ${path}/startup.sh
        fi
        ;;
  stop)
        ${path}/shutdown.sh
        del_start ${path}
        sleep 1
        ;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac

tomcat远程启停脚本

9月 27

我的 tornado 启停脚本

  之前在tornado学习中(一)中,写了一个启停的脚本,来启动多个端口,但是其中没有日志部分。我将脚本修改一下,使其能支持启动输出日志。具体可见早起笔记http://www.simonzhang.net/?p=1170

#!/bin/sh
#
# Filename:    main.sh
# Revision:    1.1
# Date:        2012-09-27
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
#
### END INIT INFO

# Source function library.
. /etc/profile

# Set the base value
listen_line=1
listen_start=8000
## info|warning|error|none   
loglevel='info'
log_file_prefix='logs/pypixshow.log'
log_file_max_size=20480

# 
CWD=`pwd`
cd $CWD

# See how we were called.
case "$1" in
  start)
        /bin/rm -rf main.port
	for (( i=0 ; i<${listen_line} ; i++)); do
            listen_port=$[${listen_start}+${i}]
            echo ${listen_port} >> main.port
            python main.py ${listen_port} ${loglevel} ${log_file_prefix} ${log_file_max_size} &
	done
        echo "start ok !"
        ;;
  stop)
        get_port_line=`/bin/cat main.port`
        for i in ${get_port_line};do
             now_pid=`/bin/ps -ef|grep ${i}|grep -v grep|awk ' ''{print $2}'`
             /bin/kill -9 $now_pid
        done
        /bin/rm -rf *.pyc
        echo "stop"
        ;;
  status)
        get_port_line=`/bin/cat main.port`
        for i in ${get_port_line};do
             now_pid=`/bin/ps -ef|grep ${i}|grep -v grep`
             if [ -z "${now_pid}" ] ; then
                 echo ${i} "is stop"
             else
                 echo ${now_pid}
             fi
        done
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit $rc

main.py 也需要修改,下面简单列出日志相关的代码。

import  tornado.options
import logging

if __name__ == "__main__":
    listen_port =  sys.argv[1]
    tornado.options.options['logging'].set(sys.argv[2])
    tornado.options.options['log_file_prefix'].set(sys.argv[3])
    tornado.options.options['log_file_max_size'].set(int(sys.argv[4]))
    tornado.options.parse_command_line()
    application.listen(listen_port)
    tornado.ioloop.IOLoop.instance().start()
6月 17

tornado学习笔记(一)

学习地址 http://www.tornadoweb.cn/documentation
http://sebug.net/paper/books/tornado/#tornado-walkthrough。
本文只是学习笔记

一、部署

  开始学习。服务器使用内部pc服务器,地址为192.168.1.41,操作系统为centos 5.6,python2.6。安装tornado环境太简单了,两条命令如下:
# easy_install tornado
# yum install pycurl

  环境安完,开始学习。按照说明拷贝代码命名为main.py代码如下:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

运行服务
python main.py

直接在浏览器中输入http://192.168.1.41:8888,看到了经典的东西了。tornado是单进程,在多核服务器上,我器多个进程,然后用nginx做负载。自己写了一个启停服务的脚本。

启停服务脚本如下,命名为”main.sh”,运行脚本后会在同级目录产生一个“main.port”文件,文件中记录启动的端口号列表。

#!/bin/sh
#
# Filename:    main.sh
# Revision:    1.0
# Date:        2012-06-14
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
#
### END INIT INFO

# Source function library.
. /etc/profile

# Set the base value
listen_line=4
listen_start=8880

# 
CWD=`pwd`
cd $CWD

# See how we were called.
case "$1" in
  start)
        /bin/rm -rf main.port
	for (( i=0 ; i<${listen_line} ; i++)); do
            listen_port=$[${listen_start}+${i}]
            echo ${listen_port} >> main.port
            python main.py ${listen_port} &
	done
        echo "start ok !"
        ;;
  stop)
        get_port_line=`/bin/cat main.port`
        for i in ${get_port_line};do
             now_pid=`/bin/ps -ef|grep ${i}|grep -v grep|awk ' ''{print $2}'`
             /bin/kill -9 $now_pid
        done
        echo "stop"
        ;;
  status)
        get_port_line=`/bin/cat main.port`
        for i in ${get_port_line};do
             now_pid=`/bin/ps -ef|grep ${i}|grep -v grep`
             if [ -z "${now_pid}" ] ; then
                 echo ${i} "is stop"
             else
                 echo ${now_pid}
             fi
        done
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 1
esac

exit $rc

  将启动文件也做一下修改,mian.py代码如下:

#!/bin/python
#-*- coding:utf-8 -*-
# Filename:    main.py
# Revision:    1.0
# Date:        2012-06-14
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import sys
import tornado.ioloop
import tornado.web


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")


application = tornado.web.Application([
    (r"/", MainHandler),
])


if __name__ == "__main__":
    listen_port =  sys.argv[1]
    application.listen(listen_port)
    tornado.ioloop.IOLoop.instance().start()

  现在配置nginx,只列出了http中的连接配置

   #######################www.grabproxy.com
   upstream  www_test_com {
            server 192.168.1.41:8880;
            server 192.168.1.41:8881;
            server 192.168.1.41:8882;
            server 192.168.1.41:8883;
       }
   server {
        listen       80;
        server_name 192.168.1.41;
        location / {
        proxy_cache_key $host$uri$is_args$args;
        proxy_redirect          off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        Host $http_host;
        client_max_body_size   10m;
        proxy_connect_timeout  300;
        proxy_send_timeout     300;
        proxy_read_timeout     300;
        proxy_buffer_size      16k;
        proxy_buffers          4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
        access_log  logs/access.log  main ;
        access_log on;
        proxy_pass              http://www_test_com;
        }
     }

  重新加载nginx配置,在浏览器里输入http://192.168.1.41,又看到经典页面。 

简单做个压力测试,结果如下,cup主频3.0。

# ./ab -c1000 -n10000 http://192.168.1.41:8880/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.41 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

Server Software: TornadoServer/2.1.1
Server Hostname: 192.168.1.41
Server Port: 8880

Document Path: /
Document Length: 21 bytes

Concurrency Level: 1000
Time taken for tests: 8.013 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 1790000 bytes
HTML transferred: 210000 bytes
Requests per second: 1247.92 [#/sec] (mean)
Time per request: 801.331 [ms] (mean)
Time per request: 0.801 [ms] (mean, across all concurrent requests)
Transfer rate: 218.14 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 166 680.6 0 3004
Processing: 20 115 46.5 100 873
Waiting: 20 115 46.5 100 873
Total: 47 281 686.0 100 3325

Percentage of the requests served within a certain time (ms)
50% 100
66% 101
75% 106
80% 124
90% 290
95% 3105
98% 3134
99% 3149
100% 3325 (longest request)

但是通过nginx负载后性能却下降,部分结果如下:
Failed requests: 9609
(Connect: 0, Receive: 0, Length: 9609, Exceptions: 0)
Write errors: 0
Non-2xx responses: 391
Total transferred: 2256920 bytes
HTML transferred: 274515 bytes
Requests per second: 714.81 [#/sec] (mean)

总结:tornado性能不错,但是nginx负载后直接使用proxy转发,耗费在连接上的资源比较高。这个和资料里介绍的出入较大,以后再找详细原因。如果单从性能讲nginx+uwsgi+django的组合效率不错。如果要使用tornado在多核服务器上工作,有两个方案,一使用lvs做底层的负载,二在开发中在代码里使用多线程进行处理。tornado对于wsgi文档中原文“Tornado 对 WSGI 只提供了有限的支持,即使如此,因为 WSGI 并不支持非阻塞式的请求,所以如果你使用 WSGI 代替 Tornado 自己的 HTTP 服务的话,那么你将无法使用 Tornado 的异步非阻塞式的请求处理方式。比如 @tornado.web.asynchronous、httpclient 模块、auth 模块,这些将都无法使用。”还要说明,node.js是非常快,但是node.js+express后效率并不理想,闲人可以测试一下。

听说gevent也不错,但是粗看了一下,没有看到中文文档。tornado对于个人来说,这个性能已经满足需要了。开始进一步学习。

3月 15

linux定时重启单台上多个tomcat

tomcat长期运行会越来越慢,所以需要定时重启单台上多个tomcat,写一个脚本放在crontab定时运行。脚本如下,需要注意脚本中使用了了linux中的mail命令,在tomcat启动失败时可以发送提醒邮件。(不知道为什么贴到这里格式会乱,只能手动整理)

#!/bin/sh
# ——————————————————————————-
# Filename: tomcat_reboot.sh
# Revision: 1.0
# Date: 2010-03-01
# Author: simon
# Email: simon-zzm@163.com
# Description:
# ——————————————————————————-
# Source function library.
. /etc/init.d/functions
source /etc/profile

#######set tomcat patch
tomcat_path=(“/program/tomcat_a/” “/program/tomcat_b/”)

#######set tomcat_key patch
tom_key_path=(“tomcat_a” “tomcat_b”)

###### set bases data
admin_mail=simon-zzm@163.com
IP=`/sbin/ifconfig |grep ‘inet addr:’|grep -v ‘127.0.0.1’|awk ‘ ”{print $2}’`

###### send alert mail
send_mail()
{
echo ${IP}” “${1}” can not stop…” >${2}tom_mail.log
/bin/mail -s ${1}’-‘${IP} ${admin_mail}<${2}tom_mail.log
/bin/rm -rf ${2}tom_mail.log
}

###### delete tomcat`s work dir and startup server
del_start()
{
/bin/rm -rf ${1}work/*
$1bin/startup.sh
}
######main()
for (( i=0 ; i < ${#tomcat_path[@]} ; i++ ))
do
${tomcat_path[i]}bin/shutdown.sh
sleep 3
get_pid_count=`/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|wc -l`
if [ ${get_pid_count} -gt 0 ] ; then
/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|awk ‘ ”{print $2}’|xargs kill -9
sleep 5
get_pid_count=`/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|wc -l`
if [ ${get_pid_count} -gt 0 ] ; then
send_mail ${tom_key_path[i]} ${tomcat_path[i]}
else
del_start ${tomcat_path[i]}
fi
else
del_start ${tomcat_path[i]}
fi
sleep 3
get_pid_count=`/bin/ps -ef|grep -v grep|grep ${tom_key_path[i]}|wc -l`
if [ ${get_pid_count} -lt 1 ] ; then
send_mail ${tom_key_path[i]} ${tomcat_path[i]}
fi
done