侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

python 实现nginx日志切割

2023-11-29 星期三 / 0 评论 / 0 点赞 / 43 阅读 / 1157 字

#!/usr/bin/env python__author__ = 'linsanxian'# -*- coding: utf-8 -*-import osimport datetimeimport

#!/usr/bin/env python__author__ = 'linsanxian'# -*- coding: utf-8 -*-import osimport datetimeimport shutilfrom subprocess import check_calllog_path = '/var/log/nginx/'nginx_pid = '/var/run/nginx.pid'access_log_name = 'access.log'error_log_name = 'error.log'def yesterday():    yesterday = datetime.datetime.now() - datetime.timedelta(days=1)    return yesterday.strftime('%Y%m%d')def access_log():    new_access_log_path = log_path + access_log_name + "_" + yesterday()+ ".log"    shutil.move(log_path + access_log_name, new_access_log_path)    check_call('''gzip {_new_access_log_path}'''.format(        _new_access_log_path=new_access_log_path    ), shell=True)def error_log():    new_error_log_name = log_path + error_log_name + "_" + yesterday()+ ".log"    shutil.move(log_path + error_log_name, new_error_log_name)    check_call('''gzip {_new_error_log_path}'''.format(        _new_error_log_path=new_error_log_name    ), shell=True)if __name__ == '__main__':    command = 'kill -USR1 `cat %s `' %nginx_pid    access_log()    error_log()    os.system(command)

 

广告 广告

评论区