0

In search of occasional boot problems, I would like to keep in the log files more than 2 ones that are kept now. For example, in /var/log, I have Xorg.0.log and Xorg.0.log.old that are created by two boot ups and I need to keep track of for example last 10 log files. Similar for other main log files like dmesg (now it is dmesg and dmesg.0).

2 Answers 2

1

The following script will copy a defined logfile (source) up to a number you can define (max_number, up to 99 as the script is now) into a directory you can set (dest). The log files will be named "01logfilename", 02logfilename etc, up to the number you define.

If the max number of copies is reached, the script will always keep the last (defined) number of logs, renaming the files each time the script is run.

You can run the script by adding it to your startup applications (Dash > Startup Applications). Note that you need to have sufficient permissions on the destination if you run it on login.

The script

#!/usr/bin/python3
import os
import shutil

source = "/path/to/logfile"
dest = "/path/to/destination/to_keep_the_logs"
max_number = 10 # set number up to 99

files_indest = [i for i in sorted(os.listdir(dest)) if not i.endswith("~")]
sections = [(item[:3], item[3:]) for item in files_indest]
number_oflogs = len(files_indest)

def convert_figures(number):
    if len(str(number)) < 2:
        return "0"+str(number)
    else:
        return str(number)

def lognumbers():
    return [convert_figures(number+1) for number in range(0, number_oflogs+1)]

lognumbers = lognumbers()

if number_oflogs >= max_number:
    os.remove(dest+"/"+files_indest[0])
    for index in range(1, number_oflogs):
        shutil.move(dest+"/"+files_indest[index],
        dest+"/"+lognumbers[index-1]+files_indest[index][2:])
    shutil.copyfile(source, dest+"/"+lognumbers[-2]+source.split("/")[-1])
else:
    shutil.copyfile(source, dest+"/"+lognumbers[-1]+source.split("/")[-1])

How to use

  • Copy the script and paste into an empty textfile, safe it as logscript.py
  • Set the source (log) file after source (e.g. source = "/var/log/Xorg.0.log")
  • Set the destination folder, to keep the log files, after dest
  • Set the max number of logfiles to keep
  • Add the following command to your Startup Applications: python3 /path/to/script/script.py

No need to say that if you want / need to run the script before login, you need to start it on another level.

0

Ubuntu runs logrotate in a cron job which takes care of /var/log. Seems they profile it for desktop vs server ( server keeps the last 8 logs of everything if I recall). Here's the man to that.. http://manpages.ubuntu.com/manpages/jaunty/man8/logrotate.8.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .