Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 3 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! –  seaworthy 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. –  seaworthy Sep 25 '12 at 3:32
    
@terdon, good stuff! thank you very much! –  seaworthy Sep 25 '12 at 3:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.