LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Does cron/anacron retry failed tasks? (https://www.linuxquestions.org/questions/linux-general-1/does-cron-anacron-retry-failed-tasks-629059/)

enigma_0Z 03-18-2008 07:00 PM

Does cron/anacron retry failed tasks?
 
I'm writing a backup script, and I'm wondering if there's a retry mechanism built into cron/anacron? If not, is there a good way to build that into a cron/anacron script?

blackhole54 03-19-2008 06:20 AM

I don't know of any retry mechanism. You could have your backup script itself retry. If you want the retry to take place after some delay, you can have the script submit itself for future execution using the at command. (Edit: make sure you include a mechanism to prevent multiple instances from running if you have repetitive failure.)

There's a variation on this where you could have crond submit the job more frequently than you really want to run it, but have the script keep track of whether to do anything. For example, suppose you want to backup daily. You could have the script run hourly and touch a particular file when it succeeds. At the start of the script, it checks to see if that file exists and has a timestamp from today. If it does, it immediately exits. There are endless variations on that to meet your particular requirements.

enigma_0Z 03-19-2008 08:11 AM

This is a laptop, so I really didn't want to touch any files because if the system is off AC power, I don't want to do a backup (no use waking up the hard disk). Furthermore, if it is not connected to the backup drive, then the script will also fail. These are fairly common occurances with my laptop.

I was actually thinking of using the at command.... But does the at queue survive a reboot?

For example, if I have a cron job scheduled for 12:00 Noon every day, then if it fails (probably for the above reasons), I'd like to reschedule it anywhere from 2 to 4 hours (I haven't decided yet). So my question(s) are:

1. Does the at queue survive a reboot
2. If the task expires while the system is down (eg. I scheduled something for an hour from now, but an hour from now the system was not on), what happens?

Tinkster 03-19-2008 12:32 PM

Quote:

Originally Posted by enigma_0Z (Post 3093772)
I was actually thinking of using the at command.... But does the at queue survive a reboot?

1. Does the at queue survive a reboot

yes
Quote:

Originally Posted by enigma_0Z (Post 3093772)
2. If the task expires while the system is down (eg. I scheduled something for an hour from now, but an hour from now the system was not on), what happens?

Never tried that ... but I'd guess the job remains in
the queue, never to complete.


Cheers,
Tink

blackhole54 03-19-2008 08:43 PM

Quote:

Originally Posted by enigma_0Z (Post 3093772)
This is a laptop, so I really didn't want to touch any files because if the system is off AC power, I don't want to do a backup (no use waking up the hard disk). Furthermore, if it is not connected to the backup drive, then the script will also fail. These are fairly common occurances with my laptop.

If I understand correcty what your concern is, you can also have the script call on_ac_power and exit immediately if on battery. on_ac_power will return:

Code:

EXIT STATUS
      0 (true)  System is on mains power
      1 (false) System is not on mains power
      255 (false)    Power status could not be determined

Quote:

2. If the task expires while the system is down (eg. I scheduled something for an hour from now, but an hour from now the system was not on), what happens?
If you are talking about a task submitted with the at command, if it cannot run (for any reason) when scheduled, it will run when it can. So if it is scheduled when system is off, it will run when you boot assuming atd is running and the load average is sufficiently low. (atd will not start new jobs when the load average is above a threshold determined at compile time. The compile time value can be overridden with a command line parameter when starting atd.) But in any case, the job will stay on the queue until it runs or you remove it. If the system in stopped after the job starts running but before completion, I am not sure what happens. (While running, querying the queue will still show the job, but with a status of "running".) Depending on the laptop's configuration, atd may not run while on battery.

EDIT: I was wrong. It is anacron that I have seen configured not to start when on battery rather than atd. However, if your computer works with laptop-mode enabled (TMK, it still causes some computers to "hang"), you can use laptop mode to dissable crond and/or atd while on battery. (From your posts, it sounds like you have enough knowledge to make this decision.) Even if laptop-mode does not work with your computer, I think you could accomplish the same thing with a small amount of hacking the init scripts and utilizing the /etc/acpi/ac.d and /etc/acpip/batter.d directories. (Do your own search as I haven't done this myself. But I have suppressed crond with laptop-mode.)

JZL240I-U 03-20-2008 08:49 AM

A combination of "AT" and "fcron" might do the trick. "Fcron" can schedule its jobs relative to the time since boot up and many more nice things...

http://fcron.free.fr/

enigma_0Z 03-20-2008 09:45 AM

Thanks for all the help.

I've decided to use at, and wrote a wrapper script so tasks can reschedule themselves.

Quote:

If you are talking about a task submitted with the at command, if it cannot run (for any reason) when scheduled, it will run when it can.
An experiment confirms this, thanks.

As far as tasks rescheduling themselves too much, if at any time a backup process is initiated when one is already running, I've written my script in such a way that it does not reschedule itself, so if I get a bunch built up, in theory, on the next reschedule point, they should all empty out at once.

Last question, about anacron & cron...

If I have a cron job scheduled for a particular time (such as a weekly backup), and the system is off when that job is scheduled, what happens? Will the job simply not be executed, or does anacron automatically pick up cron's slack?? Or should I just do an experiment to test this??

[edit]
In fact, I've decided for the time being to use at exclusively, and have tasks reschedule themselves based on whether or not the backup was successful. I'm still curious about the above cron/anacron questions, though. Thanks.

blackhole54 03-21-2008 12:09 AM

Quote:

Originally Posted by enigma_0Z (Post 3095023)
If I have a cron job scheduled for a particular time (such as a weekly backup), and the system is off when that job is scheduled, what happens? Will the job simply not be executed, or does anacron automatically pick up cron's slack?? Or should I just do an experiment to test this??

I've been using Linux over 4 years, and during that time observing when cron jobs do and do not run. I frequently suspend and power down machines, so you might think I would know the answer to your question. But I don't. At least not a good one. I have observed that crond sometimes goes back and "picks up a missed job" after the computer resumes execution. But I still don't know a formula for this. I have always assumed that it depended how far back in time the "missed" job was. (The cautionary tale here is that don't assume you have it figured out by observing it once, or a few times.) If anybody else has a more definitive answer, I'd love to here it. (I suppose one could always "use the source Luke, use the source." ;) )

OK. In previewing my post, I see I slightly misread yours. What I said above about crond is to the best of my knowledge true. But you asked about anacron. Anacron is a different beast that is logically separate from crond. Based on its own configuration file (/etc/anacrontab), anacron will schedule tasks that have not been run within the prescribed number of days (based on its own timestamp files). Neither anacron nor crond directly reference what the other does. But distros frequently provide linkage between the two such as:
  • Sometimes (freqently?) distros will include scripts in /etc/cron.daily and /etc/cron.weekly that update the corresponding anacron time stamp files so anacron will act accordingly.
  • Ubuntu (and maybe other distros) has crond call anacron once a day, so tasks scheduled in anacrontab will get run even if the computer is running continuously.
anacron was created as an addition to solve the problem of cron not being designed for handling interuption of operation. Just glancing at the fcron website, I note that it is intended to solve the same problem by replacing cron (and also anacron) rather than as an addition. It looks interesting, but I haven't examined it in depth yet.

@JZL240I-U,

Thanks for the heads up about fcron. Its the first I've heard of it.

JZL240I-U 03-26-2008 04:58 AM

I use fcron in combination with rsnapshot for backups in my home used workstation. First backup will run 2 minutes after boot (presumably before I change too many files), then every hour. Else daily, weekly and monthly backups are run, all scheduled by fcron.

Nifty thing, fcron.

But I never touched cron(d), not knowing where in the system it is used by default. Never repair a runnung system...


All times are GMT -5. The time now is 07:08 AM.