[SOLVED] - logrotate monthly at midnight a custom file

Issues related to applications and software problems
User avatar
warron.french
Posts: 616
Joined: 2014/03/27 20:21:58

Re: logrotate monthly at midnight a custom file

Post by warron.french » 2020/05/04 13:19:05

pjsr2 wrote:
2020/05/04 13:02:20
Crontab does not provide a special method to run a job at the last day of the month. So if you can live with starting at midnight, 00:00:00 of the first day is easier:

Code: Select all

0 0 1 * * myjob.sh
If you really want at the end of the day, you can do something like:

Code: Select all

59 23 30 4,6,9,11        * myjob.sh
59 23 31 1,3,5,7,8,10,12 * myjob.sh
59 23 28 2               * myjob.sh
but that is not correct in leap years, where you want to have Feb. 29th.

Something like

Code: Select all

59 23 28-31 * * [[ "$(date --date=tomorrow +\%d)" == "01" ]] && myjob.sh
could also do the trick. (Note escaping the % in the command, as % is special in crontab.) But for clarity, I would run it at 00:00:00 of the first of the month. The processing will take some time anyhow, so you will end up with some log messages from the day before or the next day. If that is unacceptable, you have to do special processing of the log data to filter it.
Your second suggestion with the date command seems more promising actually; we know as of Friday where I work how to calculate what the last date of the last month was too.
Thanks,
War

User avatar
warron.french
Posts: 616
Joined: 2014/03/27 20:21:58

Re: logrotate monthly at midnight a custom file

Post by warron.french » 2020/05/16 07:30:29

TrevorH and pjsr2, a little progress on this.

I did figure out how to get all of my data for precisely the 24-hour period I wanted, all I needed to do was add a delay (sleep) inside my shell script that generates the data.

psjr2, I am going to test your [[ date ]] scripting for my needs to as it looks very promising to me. Thanks, because I have never seen the --date option used with today, tomorrow, yesterday, etc.. That is a great feature! Thanks for exposing that to me.
Thanks,
War

pjsr2
Posts: 614
Joined: 2014/03/27 20:11:07

Re: logrotate monthly at midnight a custom file

Post by pjsr2 » 2020/05/17 10:29:36

Thanks for letting us know that the reactions in the thread were useful for you.
You can mark the thread as solved by editing the subject line and adding "[Solved]" to the subject.

User avatar
warron.french
Posts: 616
Joined: 2014/03/27 20:21:58

Re: logrotate monthly at midnight a custom file

Post by warron.french » 2020/06/01 15:23:47

I will mark this thread as solved pjsr2.

The crontab entry had to be adjusted. Because the crontab execution of my logrotate policy that was not to be placed in /etc/logrotate.d/ was being executed from /root/ and the execution test before the logrotate command was looking for tomorrow's date to be "01" had executed at 00:00 on the 31st, as opposed to 2359hours. So, I adjusted the crontab to be what I needed successfully.

I wanted to share my results up in Github.com as a project, in case anyone might want to do the same thing.
By the way, in the end my crontab simply executes at 0000 on the first day of each month, my logrotate.systemup file helps name the file appropriately too.

https://github.com/dimndskier/SYSTEMUP

Thanks guys for your help.
Thanks,
War

Post Reply