Featured Image Terminal

Crontab | Automatic purging of Logs

One of the neat things that you can do in Linux or Unix for that matter is the ability to schedule events with Cron. This isn’t exactly the easiest system to understand but when you study it a bit, it is pretty simple. My favorite reference for this is on the openSUSE wiki.

https://en.opensuse.org/SDB:Cron

This is just a quick reference for me and maybe you to funnel my search to the right spot. The openSUSE Wiki is really the reference. This is an over glorified bookmark.

The Problem

I have a system that I maintain, one I did not build, that generates a lot of logs and need to be periodically purged. I’m sure there is a better way to do this but what I want to do is just clear out those logs once a week.

I could log into each of these systems, once a week to do the task, or I can create a weekly automation to just remove the log file. It is such a small trivial task that having automated will make my life just a bit easier.

Cron Format

To access the “Crontab”, in the terminal, simply run this:

sudo crontab -e

There are six fields in the crontab that need to be understood which looks like this.

* * * * * /your/command/here

The asterisks define the various time intervals which can be understood like this:

Here is another way to represent it:

Cron Cutsheet

This allows you to specify when exactly you would like whatever task it is to happen at the time of your choosing. Since the purpose of this article is not much more than my notes for doing a simple task that I can refer back do, I will cut right to the solution.

Further reading on the details and how to use Crontab can be found here:

https://en.opensuse.org/SDB:Cron

Just a note, for system wide cronjobs, you need to run crontab as root. If you want to run a cronjob as a user, just run crontab -e without any “sudo” in front of it.

My Solution

Since I needed to delete a log file that grows crazy large and locks up this Linux VM running various tasks dealing with emails, I just want to go in and delete the log file weekly. I have chosen Friday at 15:00 (3p) so that should I choose to check any logs during the week, I can do so before I am done working on Friday but not so late that I can’t double check to make sure that the cronjob is working like it should and I can verify before I walk away and do some fun family time on a Friday night.

This was very simple, at the zeroth minute, 3pm (15:00) on a Friday, I am running the command “rm /tmp/imap.log”

0 15 * * Fri rm /tmp/imap.log

And that is all I needed! I realize this is just a kind of patch or work around but I do want those logs for the times I do end up having an issue to review. For now, I can just delete them every week.

Final Thoughts

Crontab is pretty neat. It’s a great way to automate tasks on your Linux system… or any other *nix, really. Sure, it isn’t a graphical, user friendly, point and click method of automating tasks (maybe there is something, no idea, I haven’t checked) but the syntax is easy to understand. In fact, if you like doing scripting, you can very easily use crontab to run scripts of any kind at the system or user level as you see fit which is really very cool.

This is one of those traditional Linuxy things that I really love about this operating system. This kind of function is great to have when needed an maybe not necessary for everyone. Since I don’t do things like this all that often, crafting this little reference is basically all I need when I have such a need pop up again.

Another reason why Linux is fun!

References

https://en.opensuse.org/SDB:Cron


Discover more from CubicleNate.com

Subscribe to get the latest posts sent to your email.


Comments

One response to “Crontab | Automatic purging of Logs”

  1. […] Crontab | Automatic purging of Logs […]

Leave a Reply

Discover more from CubicleNate.com

Subscribe now to keep reading and get access to the full archive.

Continue reading