Issue
I built a web service using tornado and it serves days and nights. I used the command to start my service:
nohup python my_service.py &
The service log is able to write to nohup.out
. However, the file becomes bigger as time goes. I want to know how can I manage it more conveniently? For saying, using an automatic method to generate the log files with proper names and size? Such as:
service_log_1.txt
service_log_2.txt
service_log_3.txt
...
Thanks.
Solution
@jujaro 's answer is quite helpful and I tried logging
module in my web service. However, there are still some restrictions to use logging in Tornado
. See the other question asked.
As a result, I tried crontab
in linux to create a cron job at midnight (use crontab -e
in linux shell):
59 23 * * * source /home/zfz/cleanlog.sh
This cron job launches my script cleanlog.sh
at 23:59 everyday.
The contents of clean.sh
:
fn=$(date +%F_service_log.out)
cat /home/zfz/nohup.out >> "/home/zfz/log/$fn"
echo '' > /home/zfz/nohup.out
This script creates a log file with the date of the day ,and echo ''
clears the nohup.out
in case it grows to large. Here are my log files split from nohup.out by now:
-rw-r--r-- 1 zfz zfz 54474342 May 22 23:59 2013-05-22_service_log.out
-rw-r--r-- 1 zfz zfz 23481121 May 23 23:59 2013-05-23_service_log.out
Answered By - zfz Answer Checked By - Robin (WPSolving Admin)