LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-18-2008, 08:00 PM   #1
enigma_0Z
Member
 
Registered: Apr 2005
Distribution: Ubuntu, RHEL, Darwin
Posts: 73

Rep: Reputation: 15
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?
 
Old 03-19-2008, 07:20 AM   #2
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
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.

Last edited by blackhole54; 03-19-2008 at 07:23 AM.
 
Old 03-19-2008, 09:11 AM   #3
enigma_0Z
Member
 
Registered: Apr 2005
Distribution: Ubuntu, RHEL, Darwin
Posts: 73

Original Poster
Rep: Reputation: 15
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?
 
Old 03-19-2008, 01:32 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913Reputation: 913
Quote:
Originally Posted by enigma_0Z View Post
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 View Post
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
 
Old 03-19-2008, 09:43 PM   #5
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by enigma_0Z View Post
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.)

Last edited by blackhole54; 03-19-2008 at 10:14 PM. Reason: power is off -> system is off
 
Old 03-20-2008, 09:49 AM   #6
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 18.3+19.0, MX-18
Posts: 4,258

Rep: Reputation: Disabled
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/
 
Old 03-20-2008, 10:45 AM   #7
enigma_0Z
Member
 
Registered: Apr 2005
Distribution: Ubuntu, RHEL, Darwin
Posts: 73

Original Poster
Rep: Reputation: 15
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.

Last edited by enigma_0Z; 03-20-2008 at 12:52 PM.
 
Old 03-21-2008, 01:09 AM   #8
blackhole54
Senior Member
 
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Quote:
Originally Posted by enigma_0Z View Post
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.
 
Old 03-26-2008, 05:58 AM   #9
JZL240I-U
Senior Member
 
Registered: Apr 2003
Location: Germany
Distribution: openSuSE Tumbleweed-KDE, Mint 18.3+19.0, MX-18
Posts: 4,258

Rep: Reputation: Disabled
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Anacron job 'cron.daily' cccc Debian 15 05-20-2008 01:39 PM
Slackware 12 undevtrigger -retry failed okos Linux - Software 3 07-30-2007 08:16 AM
Trouble running couple of programs under cron/anacron dmsynck Ubuntu 6 03-04-2007 09:49 AM
Cron/Anacron Not Working - Debian Sarge br00tal Debian 2 10-10-2005 07:41 PM
cron / anacron / fcron Will Linux - General 3 11-09-2001 02:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration