2

I am a Linux newbie and I'm running the following Linux command:

nohup python my_script.py > my.out 2> my.err

and everything works as expected; all the output from my python (2.7) print statements get logged in the file called my.out.

The only problem is that after several days of running the file, my.out, gets very large. Is there a way (at the Linux shell level) I can have a new "my.out" created, say once a day, with a time stamp in the file name?

6
  • You could use logrotate Commented Jun 29, 2017 at 13:54
  • 3
    You could also rewrite your script to use Python's logging module, with the RotatingFileHandler Commented Jun 29, 2017 at 14:00
  • I strongly recommend @FamousJameous suggestion however if you want to do it on the OS level you can create a cron job. Commented Jun 29, 2017 at 14:06
  • I'll look into both of your suggestions. Commented Jun 29, 2017 at 14:19
  • Looking into RotatingFileHandler - Thanks Commented Jun 29, 2017 at 14:50

3 Answers 3

2

You should use logging module in your script :

import logging.handlers

LOG_FILE_NAME = '/var/log/my_script.log'
LOGGING_LEVEL = logging.INFO

formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
handler = logging.handlers.RotatingFileHandler(LOG_FILE_NAME, mode='a', maxBytes=5000000, backupCount=5)
handler.setFormatter(formatter)
log = logging.getLogger("my_script")
log.addHandler(handler)
log.setLevel(LOGGING_LEVEL)

In this example, we configure a log file rotation of 5MB with a retention of 5 files. So each time your log file reach 5MB, the script creates a new log file and archive the old one (5 files max).

Here is an example of code to log data in your log file :

def do_something():
    # do stuff here
    log.info("Something has been done")

Once you've done that, simply run your python script :

python my_script.py

Everything will be logged in the file you want with automatic log rotation.

If you prefer a rotation based on time, please refer to TimedRotatingFileHandler

https://docs.python.org/2/library/logging.handlers.html#timedrotatingfilehandler

Sign up to request clarification or add additional context in comments.

8 Comments

RotatingFileHandler worked very nicely. I have one question, is there a way to specify an unlimited backupCount? Thanks also to FamousJameous
@PhilO According to the documentation it's not possible. You can still backup your log files so they are not erased during log rotation.
Is there a way to log to the file and the console? The latter would help with debugging.
log will save data in your log file only. You can still redirect the std output to a file, but i dont see the point. If you make a good use of the command log you won't need to redirect console. And to debug, use a debuger :)
The log line is in a loop? If not you can ask another question for that maybe.
|
2

What you want is what Apache fellows did with rotatelogs. Extract:

rotatelogs is a simple program for use in conjunction with Apache's piped logfile feature. It supports rotation based on a time interval or maximum size of the log.

Synopsis

rotatelogs [ -l ] [ -L linkname ] [ -p program ] [ -f ] [ -t ] [ -v ] [ -e ] [ -c ] [ -n number-of-files ] logfile rotationtime|filesize(B|K|M|G) [ offset ]
...
rotationtime The time between log file rotations in seconds. The rotation occurs at the beginning of this interval. For example, if the rotation time is 3600, the log file will be rotated at the beginning of every hour; if the rotation time is 86400, the log file will be rotated every night at midnight.

For your use case, you could do:

nohup python my_script.py 2> my.err | rotatelogs -l my.out.%Y.%m.%d 86400

to have one file per day changing file at 0h00 local time.

You can find it in the Apache HTTPD project which exists for almost any common architecture

Comments

1

Yes it is, use command date to add a date inside the name of file, like :

nohup python my_script.py > my_`date +"%d_%m_%y"`.out 2> my.err

4 example I've done the next example with the following command :

echo "hello" > name_`date +"%m_%y"`

and the name of the file is name_06_17

You can see more information about date command typing :

man date

This will allow you to create different files, one per month.

hope will help!

6 Comments

Yes, but I'd like the file to be renamed each day automatically.
if you do the same command with the day in front will do this for you. You have to use doble redirection ">>" to add info to an existing file.
nohup python my_script.py > my_date +"%d_%m_%y".out 2> my.err
@MarcSànchezThis won't create a separate file for each day, but would require to rerun the command every day.
Yup, I don't want to do that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.