logit.py
March 17th, 2008A small script that rewrites the command it gets as argument, passing any further arguments on, to send stdout and stderr to a log file that is rotated on each run.
Remember to edit the logdir variable to your settings (keep a trailing / or the last part will be prepended to the logfile). The keep variable controls how many old logs should be kept. The command name is prepended to each logfile.
I.e.: logit.py xcompmgr -Cc -r2 -t-3 -l-3 -fF -D5 &
Tags: linux, python#! /usr/bin/python print """logit.py - Executes a program redirecting stdout and stderr to a rotating logfile by adding the '>' character to the command and executing it through os.system (). This software is not guaranteed to work or not do any harm to your system, use with caution. Copyright (c) 2008 Gaute Hope <eg _AT_ gaute_dot_vetsj_dot_com>""" import sys import os logdir = '/home/_you_/tmp/logs/' keep = 5 if len(sys.argv) < 2: print "You know how!" exit () name = sys.argv[1] arguments = "" for a in sys.argv[2:]: arguments = arguments + " " + a print "Executing: " + name + arguments i = keep - 1 while i >= 0: fname = logdir + name + ".log-" + str (i) if os.access (fname, os.F_OK): if i == (keep - 1): os.remove (fname) else: os.rename (fname, logdir + name + ".log-" + str (i + 1)) i = i - 1 fname = logdir + name + ".log-0" os.system (name + arguments + " > " + fname + " 2>&1") exit ()



