How to rotate a log file from crontab?

Solution 1:

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.