138

I schedule some tasks using crontab.

What will happen if my computer is shutdown or turned off during the time when cron was scheduled to do something?

Does each missed cron job run after computer is turned on, or are missed jobs for that day ignored? If missed jobs don't resume, is there any way to configure cron such that it runs the missed tasks after the computer is turned back on?

Mister_Tom
  • 446
  • 1
  • 10
  • 19
seg.server.fault
  • 1,817
  • 4
  • 16
  • 11
  • 3
    I tried looking for anacron tutorial in web but couldnt find any good ones. Looking into man pages also didnt help me much. How can I change already existing crontab so that it is changed to anacron ?? Or any tutorial in this will be very helpful – seg.server.fault Aug 10 '09 at 16:41
  • 1
    I've edited my answer to include some examples. I hope that helps! – jeff Aug 10 '09 at 16:59

5 Answers5

112

When your computer is shut down (or the cron daemon is otherwise not running), cron jobs will not be started.

If you have jobs that you would like to run after the fact during those times when the computer is shut down, use anacron. Installed by default, see "man anacron", "man anacrontab", or the file /etc/anacrontab for more info.

Ubuntu uses anacron by default for crontab entries in:

/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

leaving the remaining crontabs to be handled by the main cron daemon, specifically:

/etc/crontab
/etc/cron.d
/var/spool/cron

NOTES

Anacron itself does not run as a daemon, but relies on system startup scripts and cron itself to run.

On the Ubuntu 8.04 box I'm looking at, /etc/init.d/anacron is run at boot, and again by cron each morning at 07:30.

The README at /usr/share/doc/anacron/README.gz has a slight bit more info than is contained in the manpages.

EXAMPLES

For simple "daily", "weekly", "monthly" jobs, put a copy of or a symlink to the script in one of the /etc/cron.{daily|weekly|monthly} directories above. Anacron will take care of running it daily/weekly/monthly, and if your computer is off on the day the "weekly" scripts would normally run, it'll run them the next time the computer is on.

As another example, assuming you have a script here: /usr/local/sbin/maint.sh

And you wish to run it every three days, the standard entry in /etc/crontab would look like this:

# m h dom mon dow user  command
0 0 */3 * * root /usr/local/sbin/maint.sh

If your computer was not on at 00:00 on the 3rd of the month, the job would not run until the 6th.

To have the job instead run on the 4th when the computer is off and "misses" the run on the 3rd, you'd use this in /etc/anacrontab (don't forget to remove the line from /etc/crontab):

# period delay job-identifier command
3 5 maint-job /usr/local/sbin/maint.sh

The "delay" of "5" above means that anacron will wait for 5 minutes before it runs this job. The idea is to prevent anacron from firing things off immediately at boot time.

jeff
  • 3,006
  • 1
  • 19
  • 10
  • 5
    By default Ubuntu uses anacron. – 3dinfluence Aug 10 '09 at 16:29
  • 1
    thanks, 3dincluence -- adjusted answer to indicate which crontabs are handled by anacron in a default ubuntu setip. – jeff Aug 10 '09 at 16:34
  • 4
    if you put the script in /etc/cron.*/ rather than having its own crontab entry, don't have a "." in the script's filename. debian and ubuntu use run-parts to run such scripts, and by default run-parts will ignore files with a . in the filename - this is so that it doesn't run the .dpkg-old and .dpkg-dist versions of scripts that are renamed from upgrades. so call the script or the symlink "maint" rather than "maint.sh". – cas Aug 10 '09 at 21:58
  • [OpenSUSE uses cronie by default](https://en.opensuse.org/openSUSE:Cron_replace) from 11.2 onward (to Tumbleweed/Leap 42.2 presently). To use `anacron`, install the `cronie-anacron` package. – palswim May 18 '17 at 23:23
  • Fedora, RedHat and other derivates use cronie (rather than cron), which contains anacron as an extension (package cronie-anacron). – Dirk Jan 20 '20 at 13:11
  • In Ubuntu 20.04 in WSL2 `anacron` is not installed by default. All the `/etc/cron.*` jobs would be run by `cron` if you start the `cron` service. If you install `anacron` it will be used instead of `cron` for the daily, weekly, monthly jobs. – pabouk - Ukraine stay strong Mar 23 '21 at 21:50
23

This depends on which cron scheduler you use. The basic, vanilla cron daemon will not run tasks that were missed due to system downtime. However, there are other cron schedulers specifically designed for this situation which will do this for you. The two most common examples are anacron and fcron.

Tyler McHenry
  • 361
  • 1
  • 4
7

Tasks scheduled while the computer is off will not be run when the computer powers back on

Alan H
  • 2,234
  • 4
  • 20
  • 17
4

If you shutdown while the cron jobs are running, the system shuts down and the cron jobs stop (or don't run).

One alternative you can check into is anacron.

KPWINC
  • 11,274
  • 3
  • 36
  • 44
3

Really depends on the cron deamon you're running. Vixie-cron will just skip cronjobs that were missed. fcron will run missed crons as soon as it can. IIRC it is based on vixie-cron as well, so if that is what you're looking for you might want to check out fcron.

gregf
  • 1,010
  • 8
  • 5