方法1
-
尝试几次通过网上的touch方法都不行
-
最后得到解决思路:既然mv uwsgi.log文件后,uwsgi还是会自动找到mv后的文件继续写入,那么我就索性不再mv,直接cp,把log文件cp出来,再把原log文件的内容清空,这样曲线救国,想想其实比网上的那种方法要简单的多
-
最后通过sh脚本+crontab定时任务,每天让其00:00自动cp然后清空即可达到log文件每日备份的效果
-
脚本
-
定时任务
方法2
- python脚本实现
from apscheduler.schedulers.background import BackgroundScheduler # 子进程调度器
from apscheduler.executors.pool import ThreadPoolExecutor # 执行器
import os, time, shutil
base_path = '/home/uwsgi/log/web'
executor = ThreadPoolExecutor()
scheduler = BackgroundScheduler(executors={'default': executor})
def main():
'''
对uWSGI的系统级别的日志输出进行转移
:return:
'''
today = time.strftime("%Y-%m-%d", time.localtime())
# 得到原目标文件路径
base_file = os.path.join(base_path, 'uwsgi.log')
# 得到cp后的文件路径
new_file = os.path.join(base_path, 'uwsgi.log_{}'.format(today))
# cp 操作
shutil.copyfile(base_file, new_file)
# 清空源文件的内容
os.system('cat /dev/null > {}'.format(base_file))
if __name__ == '__main__':
# 每天凌晨执行一下任务
scheduler.add_job(main, 'cron', hour='0')
scheduler.start()
while True:
time.sleep(60 * 60 * 24)