uwsgi日志分割(踩坑实录)
今天给uwsgi项目配置日志分割,结果怎么都不生效,各种百度都无法解决,最后发现多次执行 uwsgi --ini uwsgi.ini
是不会报错的,但是会导致原来的uwsgi进程无法写入日志文件,但新的进程又没成功启动,这样日志文件就没有信息了。
只要先把原uwsgi进程kill或者执行 uwsgi --stop uwsgi.pid
, 我傻到执行错命令,执行成 uwsgi --stop uwsgi.ini
,真是晕了,下面记录一下配置信息。今天由于这个问题弄了好久!!
1、配置如下
[uwsgi]
# 这里的选项是socket / http 使用nginx再上socket, 不然就使用 http
socket = :5000
chdir = ./AudioBottle_Service/
wsgi-file = user_management.py
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:9191
pidfile = uwsgi.pid
lazy-apps = true
master-fifo = ./mfifo
touch-chain-reload = true
# master = true
# 指定监听文件,修改后重新打开日志
touch-logreopen = /home/sstest/project/touchforlogrotate
#日志路径
daemonize = /home/sstest/project/uwsgi.daemonize.log
2、定时脚本
#!/bin/bash
DIR=`echo $(cd "$(dirname "$0")"; pwd)`
LOGDIR="${DIR}/log"
echo ${DIR} >> tmp.txt
echo ${LOGDIR} >> tmp.txt
sourcelogpath="${DIR}/uwsgi.daemonize.log"
touchfile="${DIR}/touchforlogrotate"
echo ${sourcelogpath} >> tmp.txt
echo ${touchfile} >> tmp.txt
DATE=`date -d "today" +"%Y%m%d"`
destlogpath="${LOGDIR}/uwsgi.daemonize.${DATE}.log"
echo ${DATE} >> tmp.txt
echo ${destlogpath} >> tmp.txt
mv $sourcelogpath $destlogpath
touch ${touchfile}
3、设置定时任务
crondtab -e
# 在里面写入一行
0 0 * * * sh 上面的定时脚本路径
# 执行命令开启定时任务
systemctl start crond
# 设置开机自启动
systemctl enable crond
# 重启crond
systemctl restart crond
4、小tip
可以把定时任务写的小一点比如 * * * * * sh 脚本路径
,先测试是否能够实现日志分割的功能,再改回来!