Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 960
Morlock Avatar asked Sep 21 '12 17:09

Morlock


People also ask

What is anacron timer?

Anacron is used to execute commands periodically, with a frequency specified in days. Unlike cron(8), it does not assume that the machine is running continuously. Hence, it can be used on machines that are not running 24 hours a day to control regular jobs as daily, weekly, and monthly jobs.

How do you run anacron?

In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the job then decides whether to run it or not and if its timestamp is >=n(n is defined number of days) then runs it after a specified time delay.

What is anacron job?

/usr/sbin/anacron. Anacron's job is to ensure that your automation jobs are executed on a regular basis. To do this, anacron checks to find out when the last time a job ran and then checks how often you have told it to run jobs. Suppose you set anacron to run a script once every five days.


1 Answers

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.

like image 174
Barmar Avatar answered Oct 26 '22 18:10

Barmar