0

The size of my Apache access_log file is getting out of hand. So I've decided to write a script which runs every 24 hours that just removes it, I know it would have to run a command similar to.

rm /var/log/httpd/access_log-2*

I know this is a bit of a stupid question, but how would I write the script?

What file would I put it inside of and how would I make it so it automatically ran every 24 hours?

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59
Jordan Wells
  • 101
  • 14
  • 1
    Take a look at the "logrotate" utility please. It takes care of that job in a very reliable way and offers additional benefits like rotation, compression, daemon control, conditions, ... It is contained in every typical Linux distribution, so you just have to install, configure and use it. I am surprised it is not already used on your system... – arkascha Sep 16 '16 at 13:36
  • @arkascha I did look at that firstly. But for reasons I cannot remember I can't use logrotate. That's why I'm just looking to write this script which runs daily. Do you perhaps know how I'd do that? – Jordan Wells Sep 16 '16 at 13:40
  • Maybe you want to "remember" the reasons. I see little sense in re-creating something that already exists if you cannot even name the motivation for that. – arkascha Sep 16 '16 at 13:41

1 Answers1

0

You're looking for log rotation, which is a well-solved problem. You should use one of the utilities that serves this purpose instead of writing your own script. These know how to interact with the Apache process, so for example Apache will open a new file instead of continuing to write to a file that has been deleted. There are many reasons why you should not home-grow a script like this.

Since you didn't specify with Linux distribution you are using, I can't provide a more specific answer. But generally all distributions have logrotate available.

Here's a document provided by Digital Ocean that covers Ubuntu 12.04. It looks like it's still applicable to Ubuntu 16.04 (the latest LTS at this time) as well.

Robin Daugherty
  • 7,115
  • 4
  • 45
  • 59