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()
3月 19

python 脚本不能并行运行

        写了个脚本,数据需要定时循环处理,但是不能同时重复处理。也就是说,脚本需要单进程运行。脚本定时运行配置在crontab中。如果在脚本开始写状态文件,运行完成后关闭状态文件也可以,但是如果脚本中途退出,状态文件不会更改,下次运行就不容易判断了。使用进程号来判断就比较准确了,所以写了以下代码。
       以下脚本为linux下使用。

#!/usr/local/bin/python
# -------------------------------------------------------------------------------
# Filename:    my-pid.py
# Revision:
# Date:        2012-12-19
# Author:      simonzhang
# Email:       simon-zzm@163.com
# -------------------------------------------------------------------------------
import os

if __name__ == '__main__':
    try :
        #首先查看是否有pid文件
        #读取pid
        now_pid = open("./my_pid.pid","rb").read()
        #通过操作系统命令,统计pid运行的数量
        get_count = os.popen("ps -ef|awk ' ''{print $2}'|grep -w %s|wc -l"%now_pid).read()
    except:
        #如果没有pid文件,则统计为零
        get_count = 0
    #判断pid是否在进程中
    if int(get_count) > 0 :
        #如果系统进程中有pid进程,则打印后推出
        print "run...."
        sys.exit()
    else:
        #如果没有进程则获得后,保存在pid文件中
        w_pid = os.getpid()
        w_pid_file = open("./my_pid.pid","wb")
        w_pid_file.write("%s"%w_pid)
        w_pid_file.close()
        #运行脚本
         main()