Avatar of badwolfff
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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
LinuxLinux DistributionsWeb ServersMagento

Avatar of undefined
Last Comment
Zephyr ICT

8/22/2022 - Mon
Zephyr ICT

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!
Zephyr ICT

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes