Morlock
Morlock

Reputation: 7121

Run job every month on a specific day (with anacron?)

I would like to run jobs once a month on, let's say, the 22nd day of the month, on my laptop running Ubuntu 12.04.

Since it's a laptop, and I may not always even use it every 22nd day of each month, cron is not a very good option.

Looking into anacron, there seems to be a limitation. Namely, you can specify a 'period', but not a specific day of the week or day of the month, as suggested by the anacrontab file format:

# cat /etc/anacrontab
period   delay   job-identifier   command
7       15      test.daily      /bin/sh /home/myself/backup.sh

I would like to be able to say, if we're on the 22nd day of the month, and of course the laptop is running, run the job. If the 22nd is passed and you have not run the job, run it as soon as I boot.

I am about to do something ugly, like mixing cron and anacron with custom scripts or writing my own bash script, using timestamps, probably reinventing the square wheel in the process.

Any idea about a best course of action?

Cheers.

Upvotes: 8

Views: 1576

Answers (1)

Barmar
Barmar

Reputation: 780724

Run the command daily, and have the script record the date that it last performed a backup

When it starts up, get the current day of month. If it's the 22nd oh the month, run normally and save the date. If it's >22, and the last run was in the same month, exit. If it's <22, and the last run was the previous month (don't forget to account for wrapping from 12 to 1), exit.

The date should be saved in a file somewhere.

Upvotes: 5

Related Questions