yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
wget https://nginx.org/download/nginx-1.16.0.tar.gz
tar -xf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx-1.16.0 --with-http_stub_status_module --with-http_ssl_module
make && make install
/usr/local/nginx-1.16.0/sbin/nginx
tar xf Python-3.6.8.tgz
cd Python-3.6.8
./configure --prefix=/usr/local/python3
make
make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
import os, datetime, re, time
program_start = time.time()
date = datetime.datetime.now().strftime('%Y%m%d')
access_path = "/usr/local/nginx/logs/access.log"
error_path = "/usr/local/nginx/logs/error.log"
cut_dir = "/usr/local/nginx/logs/history"
# Get the size of the log
access_size = os.path.getsize(access_path)
error_size = os.path.getsize(error_path)
# convert the size from bytes to MB
def to_mb(size):
to_kb = float(size)/1024
to_mb = to_kb/1024
# remain 1 bits
return '%.1f'%(to_mb)
access_size_withmb = to_mb(access_size)
error_size_withmb = to_mb(error_size)
# get nginx_pid
def get_nginx_pid():
nginx_conf_path = "/usr/local/nginx/conf/nginx.conf"
with open(nginx_conf_path, 'r', encoding='utf-8') as f:
nginx_conf = f.read()
nginx_pid = re.search("/.*nginx.pid", nginx_conf).group(0)
return nginx_pid
# cut logs
def main_process(size_withmb, path):
if size_withmb > 500:
new_path = '%s/%s-%s'%(cut_dir, path.rsplit('/', 1)[1], date)
nginx_pid = get_nginx_pid()
print('>>>>>>>>>>>>>>>>>>>>>>>>>>the log size is %sMB, start to cut <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' %(size_withmb))
if not os.path.isdir(cut_dir):
os.system('mkdir {}'.format(cut_dir))
os.system('mv {} {}'.format(path, new_path))
print('>>>>>>>>>>>>>>>>>>>>>>>>>> cut log finished <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
print('>>>>>>>>>>>>>>>>>>>>>>>>>> log is reloading <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
os.system('kill -USR1 `cat {}`'.format(nginx_pid))
print('>>>>>>>>>>>>>>>>>>>>>>>>>> log reload finished <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
else:
print('>>>>>>>>>>>>>>>>>>>>>>>>>> log size is less than 500MB, not need to cut <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
# compress logs
def compress_log():
print('>>>>>>>>>>>>>>>>>>>>>>>>>> start to compress logs <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
res = os.system("find %s -mtime 3 -type f |xargs tar czPvf %s/log.tar-%s 2>/dev/null"%(cut_dir, cut_dir, date))
if res == 0:
print('>>>>>>>>>>>>>>>>>>>>>>>>>> compress logs finished <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
else:
print('>>>>>>>>>>>>>>>>>>>>>>>>>> no logs need to compress <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<')
if __name__ == '__main__':
main_process(access_size_withmb, access_path)
main_process(error_size_withmb, error_path)
compress_log()
execute_date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
program_end = time.time()
execute_time = '%.1f'%(program_end - program_start)
print('>>>>>>>>>>>>>>>>>>>>>>>>>> the program executed %s seconds, today is %s <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<' %(execute_time, execute_date))
print('\n')
原文:https://www.cnblogs.com/cjwnb/p/12310359.html