🔍 See Original Answer on Ask Ubuntu 🔗
Cron Monthly job works randomly using `0 0 1 * *`
January 10, 2019
Edited: June 12, 2020
November 24, 2022
Copy to clipboard: false
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.
Once per month is unpredictable. The cron job was setup on January 9, 2019 and then ran on January 30, 2019:
Hopefully the cron job runs again on Feb 1, 2019 as anticipated!
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
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…
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
20190201 using technique in Update 2.