In this article I will talk about an open-source utility that hasn’t got much airspace since it was released on our IBM i.
Its called logrotate, a handly utility that can help us manage the many logs we have on our IFS.
As we all know, log files can easily get out of hand.
How many times have our clients call us up and moaned about performance, upon investigation it becomes apparent the php.log file has never been cleared down and holds millions of records.
Logrotate is here to help you get around this problem and clean up the logs on a periodic basis.
Logrotate is very powerful. It allows automatic rotation, compression, removal and mailing of log files.
Each log file may be handled daily, weekly, monthly, or when it grows too large. All very useful.
If you are confident using a Bash session connected to your IBM i using SSH and a quick Yum install will get you going.
yum install logrotate
Or, use Open Source Package Management from Access for Client Solutions (ACS), which can be seen in the figure below.
Do you have the excellent man utility on your box, if not, why not!
See my previous PowerWire article Where’s that manual gone?
Using man, we can see the manual for logrotate.
Please be aware that the logrotate utility resides in the /QopenSys/pkgs/sbin folder, so adjust your profile path, or prefix it with this folder if you intend using this feature. For example
Logrotate works off very simple text configuration files. Create this file, in any IFS folder, using your favourite editor, VS Code for example.
In this example, I’ll create a configuration file that checks and maintains all the logfiles in my /PowerWire/Logs folder on my IBM i IFS. I’ll create a file called powerwire.conf in my /powerwire/logrotate folder, with the following entries.
This is not an extensive list by any means. Check out the logrotate command using MAN in my top-tip above.
Now we have our config file, let me test it out.
All we must do, is to run logrotate with our configuration file as the first parameter, as in
If we only want to check our configuration file is holding all the necessary details, we can run logrotate in debug mode, which will perform just a dry run and not actually perform any archiving.
To run in debug mode, just use the -d flag after logrotate. This can be seen in the figure below.
Here we can see each log file is being interrogated to check if any archiving needs to be performed.
Once you are happy with the archiving, just run the logrotate command without the -d flag and away it goes, sorting out your logs. All very neat!
If we want to perform logrotate on a scheduled basis we can easily add it to our job scheduler using the command seen below.
CMD(QSH CMD(‘/QOpenSys/pkgs/sbin/logrotate /powerwire/logrotate/powerwire.conf’))
TEXT(‘Tidy log files’)
Another great addition to the open source on IBM i catalogue, that opens the possibilities of what can be achieved on the IBM i server.
All the examples I have written for this article can be found on my open-source repository on GitHub, which can be found at https://github.com/AndyYouens/f_Learning
If you have any questions, either on this article, or anything else on the IBM i, use the comments below, or send me a message on twitter @AndyYouens
Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange experience.
IBM Champion 2021