10

I have this directive on my crontab:

* * * * * /var/www/github/mysite/bin/email email worker await --pid=/var/run/mysite-worker-email.pid >> /tmp/mysite-worker-email.log

As you can see all my logs are written in /tmp/mysite-worker-email.log, is there a way I can rotate that file?

DomingoSL
  • 365
  • 1
  • 4
  • 13
  • @Sven My system is not doing any logrotate, im just dumping the echo of a php script on a file using crontab: * * * * * myscript >> log.txt – DomingoSL Jul 06 '15 at 12:29
  • @DomingoSL the point is, the machine on which you are running your crontab entry probably does have logrotate installed. You can use logrotate to rotate any logs, including your own, and it's probably the most standard solution. Are you the sysadmin for this machine or is it shared hosting or something? – EightBitTony Jul 06 '15 at 12:43

1 Answers1

22

Assuming you have root permissions on the machine. If you are on shared hosting, SF is the wrong site for you anyway.

Check if /etc/logrotate.conf and /etc/logrotate.d exists. If they do, add a file with a fitting name, e.g. site_email into /etc/logrotate.d with a content like the following:

/tmp/mysite-worker-email.log {
   compress
   daily 
   missingok
   rotate 7
}

This will rotate the log files daily, and keep compressed copies of the last 7 days. logrotate has many more options, see man logrotate.conf for all of them.

Please note: Keeping log files in /tmp is a bad idea. Put them into /var/log/ where they belong, especially if you want to keep more copies of the log file and don't want them to get deleted when /tmp is cleaned up.

Sven
  • 98,649
  • 14
  • 180
  • 226