Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

In this tutorial, we’ll discuss what happens to cron tasks when we shut down our machine. We’ll cover different schedulers and how they behave during this circumstance.

2. Cronjobs and Shutdown

Scheduled jobs don’t run when the cron daemon isn’t running. Tasks that are scheduled by the cron service are canceled once the machine powers off.

Vanilla schedulers don’t have the ability to run the scheduled tasks after the computer turns back on. However, there are other schedulers that have this ability. We’ll discuss one such scheduler in the next section.

2.1. anacron

anacron is a cron service that’s suitable for scheduling asynchronous tasks on systems that are shut down regularly.

By default, it comes with Ubuntu and Ubuntu derivatives. However, we can also install it from our official distribution package repository using a package manager such as yum or apt:

$ apt install anacron

Similarly, we can use yum which comes with Fedora and RHEL:

$ yum install anacron

anacron doesn’t run as a daemon but relies on system startup scripts and cron itself to run. By default, anacron uses crontab entries in /etc/cron.daily/etc/cron.weekly, and /etc/cron.monthly. 

For simple daily, monthly, or weekly jobs, we can simply put a symlink to the script in one of the /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly. anacron runs the appropriate scripts accordingly.

However, if the machine were turned off the day the scripts would normally run, anacron tries to run the script the next time the machine’s turned on.

For instance, here’s the entry in the standard crontab file for a script that runs every three days:

0 0 */3 * * user /usr/bin/sync.sh

Now, if our machine is turned off on the 3rd day at 00:00:00 when the script is supposed to run, the script won’t run until the 6th day.

On the other hand, with anacron, we can make the script run on the 4th day if our machine was off on the 3rd day. We can accomplish this by adding an entry to /etc/anacrontab:

3 5 sync-job /usr/bin/sync.sh

Let’s break it down:

  • the script runs every “3” days
  • there will be a “5” minutes delay before the script is triggered
  • sync-job is the label for our task
  • /usr/bin/sync.sh is the path to our script

We placed the five minutes delay to prevent anacron from firing things off immediately at boot time.

We should keep in mind to remove the entry from /etc/crontab to make sure we don’t have duplicated tasks running at the same time.

2.2. Cronjobs and Suspended Mode

When our machine is in suspended mode, the cronjobs will fail to run. However, there’s a convenient utility called rtcwake.

rtcwake allows us to specify a wake-up time to wake our machine from sleep mode. Therefore, we can have a few cronjobs that let us wake the machine, run the required cronjobs, and then suspend the machine.

For instance, our first entry in the crontab specifies to wake the machine from sleep mode. If the rtcwake command is executed before the system is put to sleep, then the system wakes up at the given time:

1  4 * * *  sudo rtcwake -m no -t $(date +\%s -d "tomorrow 03:55")

Let’s break it down:

  • -m signifies the alert to wake up the machine. In our case, the no argument specifies that we want to do nothing if the machine is already awake
  • -t option is used to specify the time for waking up the machine. In this case, it’s 03:55 — a few minutes before the scheduled cronjob

We should know that the rtcwake command always requires root privileges. One more thing we should keep in mind is that if our system failed to execute this cron job, then our subsequent cron tasks won’t be able to run. Therefore, in this case, we’ll have to enter the command manually.

Next, in the crontab, we specify the task that we want to accomplish. As an example, let’s run our sync script:

59 3 * * * /usr/bin/sync.sh

In this scenario, the task will run at 04:01. Afterwards, we can simply suspend the machine through some energy-saving options that we can set in the settings.

Alternatively, we can also use the following command to suspend the system:

$ systemctl suspend

3. Conclusion

In this article, we learned how and when the scheduled jobs run when the computer is shutdown. We discussed the vanilla cron scheduler as well as the anacron task runner.

Finally, we also looked at the rtcwake utility that lets us run the cron jobs when our machine is suspended.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

guest
0 Comments
Inline Feedbacks
View all comments