0

I have a command that I want to run regularly, i.e. at fixed intervals (say once per day).

As far as I know, the usual approach would be to use cron. That would require me to schedule my task at specific times (say every day at midnight). In my case, however, the target machine is not running continuously, so it might not be running at the scheduled time every day.

I'm therefore looking for some kind of background service that uses the elapsed time since the last command invokation instead of a specific time when deciding whether to execute the command or not.

For example, assume that the last execution of the command was two days ago at noon and that the machine was shut down afterwards. Now the machine starts up again for the first time since then. The daemon notices that more than a day has passed since the last invokation and runs the command (no matter what the current time is). If the machine keeps running for at least 24h afterwards then the command will be executed again after 24h.

Obviously I could hack something together which is called by cron on a more frequent basis and performs the necessary checks itself, but I was wondering if there exists a ready solution for this kind of problem.

Florian Brucker
  • 224
  • 1
  • 2
  • 12
  • 1
    You can add in your cron @reboot a script which test if it need to launch your command. So each reboot your command will be launch if needed – Froggiz Nov 27 '15 at 14:44

2 Answers2

3

Use anacron, does exactly what you want, it's meant like cron for computers that are not up 24 x 7

Link: http://anacron.sourceforge.net/

Fredi
  • 2,257
  • 10
  • 13
2

You can use anacron on Linux systems.

Create a file /etc/anacrontab with the following content:

# period  delay  job-identifier  command
  7       2      cron.weekly     mycommand

If mycommand has not been run in the last 7 days, it waits 2 minutes and then runs the command.
Anacron jobs are run by crond, and are very useful for the execution of periodic jobs on a machine that is not always powered on, such as a laptop. Only the superuser can schedule anacron jobs, which have a granularity of one day (vs one minute for cron jobs).
The last execution of the specified anacron job will be saved in the file /var/spool/anacron/job_id.

dr_
  • 1,085
  • 12
  • 19