Here is the crontab job

/usr/local/bin/python2.7 /home/scripts/ >> /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?

up vote 7 down vote accepted

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

/usr/local/bin/python2.7 /home/scripts/ > /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

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/ > /home/scripts/logs/$(echo -n `whoami`_`date +\%d\%m\%y_\%H\%M\%S`).log 2>&1
  • .. 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

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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