3

Suppose I set up a cron job with anacron, or with crontab -e.

Now, what if my machine is switched off at the moment cron should have executed the scheduled job?

Would the job be executed as soon my machine is turned on? Or would it not be executed at all?

4

If you are using cron, then the job will Not be executed if the computer is off at that time.

If you are using anacron for that job, it will be executed once the computer is turned On next time. The time of execution needs some explnation. Note that the smallest unit of time from anacron is day, so you can only set an anacron job that will have a frequency to run of 1 day or more.

Here is a sample entry of /etc/anacrontab:

1       5       cron.daily      run-parts --report /etc/cron.daily
  • The initial 1 indicates that this job will run each day (frequency=1 day)

  • 5 indicates that there will be 5 minutes delay after the start of the computer before running this job

  • cron.daily is the job identifier for anacron, it is used to identify specific jobs in the spool directory /var/spool/anacron/

  • run-parts --report /etc/cron.daily is the command to be executed.

So, if your computer was turned off at the time of running the job, anacron will check the timestamp of last run from /var/spool/anacron/cron.daily. If it was executed the previous day or earlier then anacron will run the job and update the timestamp accordingly.

Ubuntu uses anacron for daily, weekly and monthly jobs. From /etc/crontab:

05 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )

As you can see everyday at 6:05 AM, the daily jobs will be run from /etc/cron.daily. If anacron exists, it will parse /etc/anacrontab (and run run-parts to execute the files under /etc/cron.daily), otherwise cron will use run-parts to directly execute all files under /etc/cron.daily.

If the computer is Off at that time, anacron will run the job after turning it On next time as per instructions from the /etc/anacrontab file.

5

cron will never ever start a job when the system is off when the time for the cron job passed. Jobs can be executed in minutes.

anacron does start a job when a system was turned off. But anacron can only be set to days. Nothing smaller.

anacron is used for desktops so Ubuntu has this active (desktops are expected to be turned off reguraly). cron is mainly used for servers (those machines tend to be on permanently).

  • Cron is present in Desktop as well; its use is less so than anacron, but still. – Thomas Ward May 29 '15 at 18:36
1

crontab as any other whatever scheduler or whatever can't work when the computer is off.

cron is a unix, utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix . Crontab (CRON Table) is a file which contains the schedule of cron entries to be run and at specified times.

So run cron run the command

 crontab -e

will open the cron file in your default editor.

Add the entry of your scheduled command

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

So now for your question, suppose I have this cron entry:

30 23 31 5 * apt-get install foo

This I want to install the foo package in 31-5-2015 at 23:30

So what happen if the computer was off at this time

in simple words, the cron is not executed and will never be executed at all

Now suppose I have this cron entry:

30 23 * 5 * apt-get install foo

This I want to install the foo package in any day of month 5-2015 at 23:30

So what happen if the computer was off at this time

in simple words, the cron is not executed, but you still have some opportunity to run in any day of Month 5 this year at the time 23:30.

As a conclusion

The cron will not just return to complete if the PC was off by the time it had to be run, instead it will execute at the specific time in your crontab entries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.