Software & AppsOperating SystemLinux

When Does Cron Execute Daily Scripts?

Ubuntu 15

Cron is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. One of the common tasks scheduled with cron is the execution of daily scripts. But when exactly does cron execute these daily scripts? The answer to this question depends on whether anacron is installed on your system or not.

Quick Answer

Cron executes daily scripts at approximately 6:25 am if anacron is not installed on the system. If anacron is installed, the execution time is controlled by the entry in the /etc/anacrontab file and the scripts run once per day with a 5-minute delay. On systems managed by systemd, anacron is started by cron at 7:30 am.

Cron Without Anacron

If anacron is not installed on your system, the cron daemon will execute the daily scripts at approximately 6:25 am. This is specified in the /etc/crontab file. The line in the crontab file that controls this execution looks like this:

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

Let’s break down this command:

  • 25 6 * * * specifies the time of execution. The five fields represent minute (25), hour (6), day of the month (* for any), month (* for any), and day of the week (* for any), respectively. So this command will run at 6:25 am every day.
  • root is the user that the command will run as.
  • test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) is the command to be run. It checks if the anacron program exists and is executable (test -x /usr/sbin/anacron). If it is not (||), it changes the current directory to the root directory (cd /) and executes the scripts in the /etc/cron.daily directory (run-parts --report /etc/cron.daily).

Cron With Anacron

If anacron is installed on your system, the execution of daily scripts is handled differently. Anacron is a program that can run commands periodically, with a frequency specified in days. It’s designed to work well on systems that don’t run 24 hours a day, to manage system tasks that need to be run daily, weekly, or monthly.

In this case, the execution time is controlled by the entry in the /etc/anacrontab file. The line in the anacrontab file that controls the execution of daily scripts looks like this:

1 5 cron.daily nice run-parts --report /etc/cron.daily

Here’s what this command does:

  • 1 specifies the frequency of execution in days. So this command will be run once per day.
  • 5 is the delay in minutes after the anacron start-up before the job is run.
  • cron.daily is the job identifier. It’s a unique name for the job.
  • nice run-parts --report /etc/cron.daily is the command to be run. It uses the nice command to run the scripts in the /etc/cron.daily directory with a lower CPU priority, which can help to minimize the impact on system performance.

Cron With Systemd

On systems managed by systemd, anacron is started by cron at 7:30 am. This can be seen in the /etc/cron.d/anacron file.

30 7 * * * root start -q anacron || :

This command starts the anacron service at 7:30 am every day.

Conclusion

To summarize, if anacron is not installed, the daily scripts will be executed at around 6:25 am. If anacron is installed, the execution time will be controlled by the entry in /etc/anacrontab and the scripts will run once per day with a 5-minute delay. On systems managed by systemd, anacron is started by cron at 7:30 am. Understanding these nuances can help you better manage and troubleshoot your cron jobs.

What is cron?

Cron is a time-based job scheduler in Unix-like operating systems that allows users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.

How does cron execute daily scripts?

The execution of daily scripts by cron depends on whether anacron is installed on the system or not. If anacron is not installed, cron executes the daily scripts at approximately 6:25 am. If anacron is installed, the execution time is controlled by the entry in the /etc/anacrontab file.

Leave a Comment

Your email address will not be published. Required fields are marked *