3

I have this sudo crontab -e job entry I setup in April then forgot about:

0   0   1   *   *     /bin/journalctl --vacuum-size=200M

What it does is keep down the size of journalctl files each month so they are only 200 MB in size (or thereabouts).

I followed these instructions:

However I only received the following e-mail on January 1st, 2019 saying the job has finally run. I don't recall receiving an e-mail after setting the job up in April 2018. Why is this monthly job only running on New Year's Eve? :

cron monthly journalctl.png

(... SNIP ...)

cron monthly journalctl end.png

At the very end it says 1.2 GB of space was freed. However it means about 1 GB was wasted until the job ran on New Year's Eve.

1 Answers1

3

The reason the job only ran on New Year's Eve is all the other first days of the month your machine was shut off at the stroke of midnight.

The easiest way is to not use sudo crontab -e but rather create a bash script in the directory /etc/cron.monthly. Make sure the script filename doesn't contain a . in it. Filenames containing a . such as Monthly job.cron or MonthlyUpdate.sh will not run.

In this case use:

sudo -H gedit /etc/cron.monthly/journal_vacuum

Insert these lines:

#!/bin/sh
#
# NAME: journal_vacuum
# DESC: Reduce size of system journals (journalctl) each month.
# DATE: January 9, 2019.

NOTE: Replaces 0 0 1 * * /bin/journalctl --vacuum-size=200M which

which only runs if machine is turned on at midnight.

/bin/journalctl --vacuum-size=200M

Save the file and exit gedit. Make the script executable using:

sudo chmod a+x /etc/cron.monthly/journal_vacuum

Now the first time the machine is turned on each month, be it 1st day of month at 7:00am or even 2nd day of month the cron job will run.


Update 1

Once per month is unpredictable. The cron job was setup on January 9, 2019 and then ran on January 30, 2019:

monthly cron job.png

Hopefully the cron job runs again on Feb 1, 2019 as anticipated!


Update 2

The script in /etc/cron.monthly/ did not run on February 1, 2019. Digging deeper I found this explanation:

Following the link's explanation I discovered:

$ sudo cat /var/spool/anacron/cron.monthly
20190130

Therefore with sudo powers I used:

rick@alien:~$ sudo -i
root@alien:~# echo 20190101 > /var/spool/anacron/cron.monthly
root@alien:~# exit

Now cron believes that last time monthly jobs were run is January 1, 2019.

Time to reboot and test...


Update 3

After reboot an email was almost instantly sent:

Anacron <XxxxxXxxxx999@gmail.com>    7:45 AM (3 minutes ago)
to root, bcc: XxxxxXxxxx999

/etc/cron.monthly/journal_vacuum: Vacuuming done, freed 0B of archived journals on disk.

Now we can see:

$ sudo cat /var/spool/anacron/cron.monthly
20190203

The last step is to replace 20190203 with 20190201 using technique in Update 2.

  • This answer relies on anacron, and anacron is not installed by default using the server installer. – pim Jan 10 '19 at 13:03
  • 1
    If you want to avoid rebooting to trigger anacron you can also simply delete /var/spool/anacron/cron.monthly and then run sudo anacron -dsq or just sudo anacron. Or use sudo anacron -f to have it run now regardless of any timestamps. On systemd systems (like Ubuntu 18.04 desktop) anacron is run by systemd units. See systemctl cat anacron.service anacron.timer. The timer unit starts the service unit every hour which in turn runs /usr/sbin/anacron -dsq and that would be the "workaround command" if rebooting isn't convenient. – PerlDuck Feb 03 '19 at 15:22
  • @PerlDuck Thank you. Your elaborate comment could be posted as a self-answered question :) PS you nominated to close this question last month so it's kind you would comment today. – WinEunuuchs2Unix Feb 03 '19 at 15:24
  • You're welcome. But the question How to [Manually run an Anacron Job](https://askubuntu.com/q/761658/504066) has already been asked ;-) – PerlDuck Feb 03 '19 at 15:31
  • 1
    @PerlDuck True but this question is specific about cron's 0 0 1 * * and how it works best for 24/7 server but not so well for home users who don't have machine turned on the first of the month at midnight. You will find 0 0 1 * * littered throughout the internet as an example of running first of month. I think the internet search will mislead many non-server users like it did myself for 8 month ends. – WinEunuuchs2Unix Feb 03 '19 at 15:37
  • I think you are right. Since I learned about those cron.Xly directories I didn't add any jobs to regular crontabs because my machine isn't up 24/7 either and anacron is just perfect for this scenario. – PerlDuck Feb 03 '19 at 15:49
  • @PerlDuck I'm open to suggestions for a new title to keep this from being closed as a duplicate....eg Why /etc/cron.* directories aren't in Ubuntu Server and are better for for non-server users than crontab – WinEunuuchs2Unix Feb 03 '19 at 16:07