How do I use a cronjob to truncate all logs on my Centos 7 server to 1 gig max size?

badwolfff used Ask the Experts™
I am running a VPS with centos 7 and have several websites sites hosted on it (wordpress, magento, joomla, opencart, corephp). I am looking for a way (cronjob if possible) that can truncate the various error.log and other log files of these sites to a max size of 1 gb each. I am imagining a script that can search for .log files on the server and truncate them at a fixed interval, if it is possible to create such a cronjob (using logrotate?). If not I don't mind setting one cronjob per error.log or whatever.log file if someone here could help me create the cronjob query that can do it.

thanks in advance
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Zephyr ICTCloud Architect

Maybe I'm missing the intended goal, but if you use logrotate you can let the logs rotate on the size you want, with the "size" parameter.

From the MAN:
Log files are rotated only if they grow bigger then size bytes. If size is followed by k, the size is assumed to be in kilobytes. If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100Gare all valid.


Yes thanks, I had heaed about logrotate but have absolutely no idea about using it.
There are many log files on my server and they are getting bulkier by the day.

How do I run or program or activate this "logrotate"?
Could you help me with the steps as I don't even know where to begin and I certainly don't want to do anything that might bugger up the whole server!
Cloud Architect
Logrotate should be in your system, you might find it under /etc/cron.daily/

Logrotate uses this file: /etc/logrotate.conf to rotate logrotate jobs you create.

It should contain something like this:
      rotate 4
      include /etc/logrotate.d
      /var/log/wtmp {
          create 0664 root utmp
              minsize 1M
          rotate 1

Open in new window

You can use this like it is, or adjust it for your needs.

Let's say you have a log called cpanel.log in /var/log, and we want to truncate it. We add a new conf file under /etc/logrotate.d/cpanel ... Enter something like following in the file:

/var/log/cpanel.log {
    size 1G
    create 0600 root root

Open in new window

And you can do that for each log

To know what all the different settings mean, I'd like to point you to the MAN

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial