之前在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> 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()