Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Here is the crontab job

/usr/local/bin/python2.7 /home/scripts/script.py >> /home/scripts/logs/log.log 2>&1

How do I generate a new log every time the job is run?

I am cool with adding an increasing counter to the log file each time it is generated. Do not know how to do this.

Do I really have to run another cron job to do that?

Thanks!

share|improve this question
up vote 6 down vote accepted

You can use the date command to generate a unique(ish) filename:

/usr/local/bin/python2.7 /home/scripts/script.py > /home/scripts/logs/"`date +\%d\%m\%y_\%H\%M\%S`".log 2>&1

EDIT: Make sure to escape the percentage signs (\%). They have a special meaning in cron and the crontab will return an error.

That way, a new log file (called <date_time>.log) will be created every time your job runs.

$ date; touch "`date +%d%m%y_%H%M%S`.log"
  Tue Sep 25 04:39:25 CEST 2012
$ ls *log
  250912_043925.log

Assuming your script does not run more often than once a second (which it can't if you are running a simple crontab), you should be fine.

If, as suggested in the comments, your script is run by different users, you can add the user name to the log file's name:

/usr/local/bin/python2.7 /home/scripts/script.py > /home/scripts/logs/$(echo -n `whoami`_`date +\%d\%m\%y_\%H\%M\%S`).log 2>&1
share|improve this answer
    
.. assuming the job only runs as one user. Otherwise, this is a good solution. – Sirex Sep 25 '12 at 2:47
    
Will try it now, thanks! – nebulus Sep 25 '12 at 3:02
    
@seaworthy There was a mistake in my orignial answer, please see my edit. – terdon Sep 25 '12 at 3:19
    
@terdon, the original did not work, trying the fixed version now. – nebulus Sep 25 '12 at 3:32
    
@terdon, good stuff! thank you very much! – nebulus Sep 25 '12 at 3:44

You must log in to answer this question.

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