uWsgi日志定时每天切割

方法1

  • 尝试几次通过网上的touch方法都不行

  • 最后得到解决思路:既然mv uwsgi.log文件后,uwsgi还是会自动找到mv后的文件继续写入,那么我就索性不再mv,直接cp,把log文件cp出来,再把原log文件的内容清空,这样曲线救国,想想其实比网上的那种方法要简单的多

  • 最后通过sh脚本+crontab定时任务,每天让其00:00自动cp然后清空即可达到log文件每日备份的效果

  • 脚本
    file

  • 定时任务
    file

方法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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值