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

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
badwolfffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
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:
size
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.
0
badwolfffAuthor Commented:
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!
0
Zephyr ICTCloud ArchitectCommented:
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:
  weekly
      rotate 4
      create
      dateext
      include /etc/logrotate.d
      /var/log/wtmp {
          monthly
          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 {
    missingok
    notifempty
    compress
    size 1G
    daily
    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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.