Views:
1,043β
Votes: 3β
β
Solution
Tag :
cron
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1108457
Title:
Cron Monthly job works randomly using `0 0 1 * *`
ID:
/2019/01/10/Cron-Monthly-job-works-randomly-using-_0-0-1-*-*_
Created:
January 10, 2019
Edited: June 12, 2020
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
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.
Update 1
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!
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.