#!/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)